Sending vars into a Javascript File .js

Discussion in 'JavaScript' started by chriswick, Feb 15, 2008.

  1. #1
    Hi Guys!


    I want to send variables into a javascript file How would I go about this?

    All I need is the code to send the var into the file.


    E.g.
    <script type="text/javascript" src="http://www.example.com/file.js?id=whatever"></script>
    HTML:
    Thanks for your help!
     
    chriswick, Feb 15, 2008 IP
  2. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #2
    Hi Chris.

    You can try the following code:
    <script type="text/javascript">id=whatever;</script>
    <script type="text/javascript" src="http://www.example.com/file.js"></script>
    Code (markup):
    Then you should be able to use the id value in file.js code.
     
    2WDH.com, Feb 15, 2008 IP
  3. chriswick

    chriswick Peon

    Messages:
    907
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply, is this how I print the variable inside the .js file?

    document.write(" +delineate2(id)); 
    HTML:
    or is that wrong?
     
    chriswick, Feb 15, 2008 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you define the variable in an earlier script tag, when you include your other file below it, it'll have access to the previous variable, so yes, it should have no trouble document.writing it.

    I also know that it is possible to pass variables through the include, I've seen it done before. You can try doing your example where you put "?id=whatever" at the end because it's likely that just might work.
     
    zerxer, Feb 15, 2008 IP
  5. 2WDH.com

    2WDH.com Active Member

    Messages:
    143
    Likes Received:
    3
    Best Answers:
    5
    Trophy Points:
    68
    #5
    Chris,

    1. "file.js?id=whatever" shouldn't work as desired with the id value unless file.js is parsed by the server.

    2. Did you check the document.write code with id variable defined in an earlier script tag? Does it work fine for you?
     
    2WDH.com, Feb 15, 2008 IP