Results 1 to 5 of 5

Thread: Need help with script

  1. #1
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Need help with script

    I'm trying to write a script that can access a variable stored in the url. Like somefile.html?something=variable
    Right now I've got a script that works but I would like to streamline it.

    My current script is
    HTML Code:
    <head>
    
    <SCRIPT LANGUAGE="javascript">
    
    
    
    function urlvar1()
    {	
    var locate = window.location
    document.locform.loc.value = locate
    var loc = document.locform.loc.value
    var start = loc.indexOf("=") + 1
    var result = loc.substring(start)
    return result
    }
    
    </SCRIPT>
    <body>
    <FORM NAME="locform">
    <INPUT TYPE="hidden" NAME="loc">
    </FORM>
    
    <script type="text/javascript" language="javascript">
    
    
    document.write(urlvar1())
    </script>
    
    
    </body>
    It works,but I want to find a way to get rid of the form. Right now I'm using it to turn window.location property into a string. Does anyone know another way to turn the url into a string. I want all the code to go in the function urlvar1() so all you do is link to the script then call the function without the extra html.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    window.location.href is a string.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Thanks I can't beleive I didn't know that. I even spent tweny minutes on google trying to find that.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Heh, no problem.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by blm126
    function urlvar1()
    {
    var locate = window.location
    document.locform.loc.value = locate
    var loc = document.locform.loc.value
    var start = loc.indexOf("=") + 1
    var result = loc.substring(start)
    return result
    }
    So this rears its ugly head again, does it?

    Someone else posted code very similar to that earlier this year after obtaining it from a very badly written article found on the Web. A better solution would use regular expressions (which I posted in that thread), though something reliable but much bulkier could be written without them.

    Does anyone know another way to turn the url into a string.
    The JavaScript 1.0 approach, which could have been used in the original article, would be concatenation with an empty string:

    Code:
    '' + location
    but the better approach (in my opinion) is using the String constructor/conversion function:

    Code:
    String(location)
    Of course, neither was ever necessary.

    Mike

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
  •