need JS that collect variable from link

Discussion in 'JavaScript' started by dracula51, Oct 12, 2009.

  1. #1
    hello. i need a JS that collects variable from link at begining of page loading

    for example look at these codes:
    suppose this is the link: http://my-domain/path/file.php?id=13
    Code (markup):
    & this is file.php code
    <html>
    <head>
    <title>my page</title>
       <script type="text/javascript">
       var theVariable = [B][COLOR="Red"]// I need this codes[/COLOR][/B]
       </script>
    </head>
    <body>
    [COLOR="Red"]// some body code, i'll write it myself[/COLOR]
    </body>
    </html>
    Code (markup):
    so, when this page load, see id=13...i mean variable "id" value is 13
    i want some JS code that collect that value (here 13) in "theVariable"

    plz help me
     
    dracula51, Oct 12, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    erm, if this is a php file then why not just...

    <html>
    <head>
    <title>my page</title>
       <script type="text/javascript">
       var theVariable = "<?=$_GET['id']?>";
       </script>
    </head>
    
    PHP:
     
    dimitar christoff, Oct 12, 2009 IP
  3. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    coz the JS will be too long...so i thought to use external source

    so that way maybe not possible
     
    dracula51, Oct 12, 2009 IP
  4. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #4
    I think that it should work, why not?
     
    astrazone, Oct 12, 2009 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    why the hell not?

    <script type="text/javascript">var theVariable = "<?=$_GET['id']?>";</script>
    <script type="text/javascript" src="myfile.js"></script>
    PHP:
    and in myfile you can totally reference theVariable.
     
    dimitar christoff, Oct 12, 2009 IP
  6. chadsmith

    chadsmith Peon

    Messages:
    82
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You'll have to use the location object for that:
    var theVariable = location.search.match(/id=([\d]+)/)[1]
    Code (markup):
     
    chadsmith, Oct 12, 2009 IP
  7. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i dont know why but its not working everytime...

    this is great...very simple trick but didnt hit my head
    its working fine :)
     
    dracula51, Oct 12, 2009 IP