SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    variable scope with external js script

    I'm trying to pass some data via JS variables to an external script, but the external script doesn't seem to see the variables defined at the top of the page. After doing some research, several posts have said that JS variables are based on a per-page basis so an external script wouldn't be able to access the variables as if the script was inline.

    What's the best way to pass variables to an external script?


    HTML Code:
    <script type="text/javascript">
    <!--
    var p_color = 'blue';
    var p_id = '35266';
    -->
    </script>
    <script type="text/javascript" src="http://www.example.com/external-script.js"></script>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    That'll work, but get rid of the HTML comments. Their entire purpose was aimed at the Mosaic browser and Netscape 1.0 - neither of which could understand scripting.

    Mosaic came out in 1993 and its userbase evaporated in 1998.
    Netscape Navigator came onto the scene in 1994 version 2.0 was released in 1996.

    You should not expect Mosaic or Netscape version 1 visitors anymore, so lose the html comments.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your reply, but the code I posted doesn't work. I tried removing the comments (thanks for that info by the way) and it still doesn't work.

    Any ideas?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    I see that you are posting example code, and not perhaps the code itself.

    If the variables are defined within a function, they will be available only within that function itself.

    It might help help to declare global variables explicitly, which also helps to clarify things when reading through the code at some later stage.

    Code javascript:
    var window.p_color = 'blue';
    var window.p_id = '35266';

    If you provide more detail about how it's actually being implemented, then more details can be given about what the cause may be.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    3MTA3
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    1,016
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply. I got it resolved. I had an extra equal sign when setting the variable so it was preventing the data from being passed.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •