Results 1 to 9 of 9

Thread: assigning javascript values

  1. #1
    Join Date
    Apr 2007
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default assigning javascript values

    Simple question that I can't seem to find the answer to.

    Can I assign a Javascript generated value to url link.
    onclick='javascriptvalue.value=1;'
    different onclick generate different values

    e.g.

    <a href="whateverpage.php?something=javascriptvalue">

    with javascriptvalue being whatever the the onclick assigns

    Thanks

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You could be more specific, here's one way to do something like that:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function aquery(l, n, v){
    var a = document.getElementById(l);
    a.href = a.href.replace(/\?.*/, '') + '?' + escape(n) + '=' + escape(v);
    }
    </script>
    </head>
    <body>
    <div>
    <a href="javascript:void(0);" onclick="aquery('main', 'var2', '32');return false;">Link Text or Image</a><br>
    <a id="main" href="some.php?var1=16">Link Text or Image</a>
    </div>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    Default

    Code:
    <a href="javascript:'whateverpage.php?something=' + (5 + 9)">
    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!

  4. #4
    Join Date
    Apr 2007
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    Code:
    <a href="javascript:'whateverpage.php?something=' + (5 + 9)">
    This would seem to be perfect but instead of going to 'whatever.php?something=14' it simply reloads the current page and writes the new url on it.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Nothing:

    Code:
    <a href="javascript:
    Is a good idea if the href ever gets executed, unless the object at that point is at least in part to unload the page.

    But that's not the main problem, as that sort of syntax is usually OK. The main problem is that it assigns nothing to anything, and just executes.

    Did you try out my idea?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    Default

    This would seem to be perfect but instead of going to 'whatever.php?something=14' it simply reloads the current page and writes the new url on it.
    Really? It should go to whatever.php?something=14. Demo page?
    Is a good idea if the href ever gets executed, unless the object at that point is at least in part to unload the page.
    This is a proper link we're talking about here, which unloads the page and loads a new one. This is what the javascript: pseudo-scheme was designed for.
    The main problem is that it assigns nothing to anything, and just executes.
    I think you misunderstood the original question: mhodgson just wants to go to an URL that has part of itself generated by Javascript. There's no assignment involved.
    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!

  7. #7
    Join Date
    Apr 2007
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    Really? It should go to whatever.php?something=14. Demo page?This is a proper link we're talking about here, which unloads the page and loads a new one. This is what the javascript: pseudo-scheme was designed for.I think you misunderstood the original question: mhodgson just wants to go to an URL that has part of itself generated by Javascript. There's no assignment involved.
    Yep, tried it again. Even started with a clean page and new page just to see if something was messing it up. It still reloads the same page with the link writen on it and does not go to the link page.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Have you tried my idea yet?

    Anyways, to do it like Twey says, you would need to tell it to do something:

    Code:
    <a href="javascript:location='whateverpage.php?something=' + (5 + 9)">
    Last edited by jscheuer1; 11-22-2007 at 09:19 AM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Apr 2007
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Have you tried my idea yet?

    Anyways, to do it like Twey says, you would need to tell it to do something:

    Code:
    <a href="javascript:location='whateverpage.php?something=' + (5 + 9)">
    Now that works as I wanted.

    Yes I have tried your idea now. It works well on a test page but the onclick event doesn't work on on my page. It could be because other things are triggered with the same onclick or that it is in a table cell with no <a href=......>.

    <td class='point widthGROUP' onclick='GoTop(); sortTable(this,1); HoverRow();' title='Sort' align='center'>
    No matter how I tried I couldn't get it to fit in and work.

    Anyway, thanks for the help, both, it works as I wanted now.

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
  •