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 7561857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:16:03+00:00 2026-05-30T13:16:03+00:00

I’m using JavascriptMVC (you don’t need to know it!) and their library steal that

  • 0

I’m using JavascriptMVC (you don’t need to know it!) and their library “steal” that manages file dependencies.

I’m a beginner with javascript and there’s something I don’t get with namespaces; I need some global variables intialized by PHP, these variables will be used in a lot of other JS files, that’s why I want to make them global:

index.php

<script type="text/javascript">
steal('jquery', function() {
   // here is some jquery specific code
   var appletVersion = '<?php echo $appletVersion; ?>';
   var baseUrl = '<?php echo BASE_URL; ?>';
});
</script>

In my JS files, I can’t access these two variables since I’ve put steal(‘jquery’, function(){ … }); and I guess they are unvisible outside of that block.

test.js

steal('jquery', function(){
   console.log(baseUrl);  // error
});
  • 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-05-30T13:16:04+00:00Added an answer on May 30, 2026 at 1:16 pm

    That’s because your two variables are local to your function that is passed to steal. My suggestion is to always refer to globals as window.globalName to make your intention clear.

    steal('jquery', function() {
       // here is some jquery specific code
       window.appletVersion = '<?php echo $appletVersion; ?>';
       window.baseUrl = '<?php echo BASE_URL; ?>';
    });
    // Now you can access window.appletVersion anywhere in your code
    

    Note that there is no need to wait for steal to grab jQuery to initialize those variables, so you could do that outside (at the global scope level).

    Namespaces

    An even better solution than using window.globalName is to create your own namespace at the global level so that you can restrict your global namespace pollution to a single object. This will help when debugging since all your code will not be mixed with the rest of the properties on the global object. Just console.log it, and you’ll have all your own globals to look at.

    var myNs = {}; // Put all your globals, classes, functions in here to avoid conflicts.
    myNs.appletVersion = '<?php echo $appletVersion; ?>' ;
    

    Printing PHP values in JS

    When you have a value in PHP and you want to print it on the page as a JS variable, you should use json_encode. Then you won’t have problems with if your string has embedded newlines, quotes or even binary data. You don’t even have to worry about the type, json_encode outputs something that is always valid to be used in JavaScript

    myNs.appletVersion = <?php echo json_encode($appletVersion); ?>;
    myNs.baseUrl = <?php echo json_encode(BASE_URL); ?>;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.