Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:59:10+00:00 2026-06-03T13:59:10+00:00

I am writing a plugin for a cms that has jQuery embedded at the

  • 0

I am writing a plugin for a cms that has jQuery embedded at the end of the html document.

I do dynamically define some variables in my template code like that:

<script>
    var name = '<?php echo $nameTakenFromTheDatabase; ?>';
</script>

This code is part of my plugin and it is not embedded on every page. Thus, name is not defined on every page.

I also do have this Javascript Code in a separate file (that gets automatically added to the page end by the cms):

var MyNamespace = (function() {

    MyClass = function(name) {
        // Do something with name
    }

    return {
        MyClass: MyClass
    }

})();

If jQuery was embedded at the top, I’d do something like this in my template code:

<script>
    var name = '<?php echo $nameTakenFromTheDatabase; ?>';

    $(document).ready(function() {
        new MyNamespace.MyClass(name);        
    });
</script>

However, with jQuery included at the bottom I can’t do that ($ is not defined yet). I as well can’t just add the jQuery-DomReady call to the separate code file, because this is executed on every page, but the name var is not initalized on every pages and breaks the code.

What can I do? Is the good old document.ready a wise approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:59:13+00:00Added an answer on June 3, 2026 at 1:59 pm

    You could define methods in you script, and execute them from the global javascript file.

    If you do something like this in you page

    <script>
        var name = '<?php echo $nameTakenFromTheDatabase; ?>';
    
        function runWhenReady(){
            new MyNamespace.MyClass(name);        
        }
    </script>
    

    Then in you global file, which is added at the bottom of the page, you can do something like this:

    $('document').ready(function() {
        if (typeof runWhenReady != 'undefined') {
            runWhenReady();
        }
    });
    

    If you are creative, you could make ‘runWhenReady’ a collection of methods, which would always be loaded when page is ready.

    EDIT I have added an example of using an array:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
    </head>
    <body>
    
        <script type="text/javascript">
            // check if array is already created. This is necessary if it is possible to render multiple pages which uses the runWhenReadyFunctions collection.
            if (typeof runWhenReadyFunctions == 'undefined'){
                window.runWhenReadyFunctions = new Array();
            }
    
            runWhenReadyFunctions.push(function(){
                $('#testDiv').text('hello world!');
            });
    
            runWhenReadyFunctions.push(function(){
                $('#testDiv2').text('hello another world!');
            })
    
        </script>
    
    
        <div id="testDiv"></div>
        <div id="testDiv2"></div>
    
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
            if (typeof runWhenReadyFunctions != 'undefined') {
                $.each(runWhenReadyFunctions, function(idx, func){
                    func();
                });
            }
        </script>
    
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some plugin code in a dll that is called by a host
I writing a plugin that does some toggling and I need a way to
I'm writing a plugin that needs some inflections of it's own. Using rails 2.3
I'm writing a small CMS as a jQuery AJAX plugin and though it's by
I've been writing a plugin for Joomla that automatically processes HTML comments eg. {dropcap}B{/dropcap}
I'm writing a plugin that will allow parameters to 'set it up.' But I
I'm writing a plugin that's modifying an existing UI. One thing I want to
I am writing a jQuery plugin and I am at the optimization stage. I
As I am writing an plugin for websites which uses jQuery I am wondering,
I'm writing a plugin which creates a custom post_type called dictionary_entry which has several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.