SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How can I use JS script to change other scripts' variables?

    Hi,

    Suppose that I have the following JS scripts inside the <body> tag:

    <script type="text/javascript"><!--
    magicVar = "whatever";
    // -->
    </script>

    <script type="text/javascript"><!--
    someVar = "whatever";
    // -->
    </script>

    Is there anyway I can change the value of magicVar in the first JS script from inside the <head> tag of the same HTML document?

    In other words:
    I want to have a JS script in the <head> of an HTML document that scans through all the <script> tags inside the <body> tag of the same HTML document, and if it finds a variable named "magicVar" inside any of the <body>'s <script> tags it'll change it's/their value to a new one. Can I use DOM or anything else to achieve this?

    I don't want to change the value of the variable magicVar, I want to change the value that will be assigned to the variable magicVar. I know I can change it manually but this is not what I need to do.

    I want to apply this to a more complicated situation.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm slightly confused. Your wanting to change the value that is assigned to a variable but you don't want to change the variables value.

    Are you wanting to change it so that anyone else that uses it will have the same value? If not, the only way I know of is to assign the variable the value you want.


  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,687
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    You cannot do what you want from javascript. I suggest that you look for a server-side solution to this instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •