Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 9 of 9

This is a discussion on Javascript Question in the Design and Development forum
Hi The link below produces a page refresh. Is there a way to add a parameter to it without breaking the javascript, and if so, ...

  1. #1
    JPC Addict
    Join Date
    Feb 2002
    Posts
    140

    Javascript Question

    Hi

    The link below produces a page refresh. Is there a way to add a parameter to it without breaking the javascript, and if so, how would it be retrieved?

    <a href="javascript:this.location.reload(); ">refresh</a>

    Thanks!

  2. #2
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,562
    You could use window.location.replace(someURL);, where in someURL you could put window.location.href plus any GET parameters you want to add.
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

  3. #3
    JPC Addict
    Join Date
    Feb 2002
    Posts
    140
    Quote Originally Posted by Gwaihir View Post
    You could use window.location.replace(someURL);, where in someURL you could put window.location.href plus any GET parameters you want to add.
    OK, thank you. What I'm trying to do is a bit complicated, so it will take some time to work out in context. I'm also poor with javascript, so there will be some trial and error, but I'll post the results back here.

    Thanks much

  4. #4
    JPC Addict
    Join Date
    Feb 2002
    Posts
    140
    Quote Originally Posted by Gwaihir View Post
    You could use window.location.replace(someURL);, where in someURL you could put window.location.href plus any GET parameters you want to add.
    Using <a href="javascript:window.location.replace (new_record.php?param=test);">refresh</a> causes the link to no longer function (nothing happens when clicked). Using <a href="javascript:this.location.reload(); ">refresh</a> will produce a link that reloads the current page, but more is needed.

    Here is what I'm trying to accomplish:

    • Provide a form to the user to begin entering information for adding any number of new records (the form allows the user to enter the number of new records to insert).

    • After submitting, pre-insert the values entered into the first form on a new form, and duplicate it x number of times, where x is the number of new records requested. (This allows the user to enter any info common to all of the new records only once, then fill in the rest of the info for each record where it differs.)

    • There are certain fields that are optional. By default these fields do not appear, only a link to add them. If the user clicks the link at this point, I need the page to refresh to reload the current page and all of the info it contains, but with a parameter I can retrive to tell me to include the optional fields now.


    If a link could be created that reloads the page AND sends a parameter (something like action=add_optional_fields), this would work.

    Is this on the right track, or is there a better way entirely to accomplish this perhaps?

    Thanks!

  5. #5
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,562
    I meant someUrl as a variable, as I assumed you wanted to set the url dynamically. To put in a literal value like that, simply add quotes around it. Oh, and loose the space after .replace. I doubt all browsers support it.

    <a href="javascript:window.location.replace ('new_record.php?param=test');">refresh</a>

    BTW: something does happen when you try yours: you get a javascript error. I can recommend using Firefox + Firebug to help you make sense of those.

    That's regarding the original question. As for the bigger picture: this won't work well. If you refresh the page like that, the user will loose all the info he has entered but not yet submitted. It would be much simpler to put all the fields in all the time, but hide those optional ones (display:none in your css). Your javascript link(s) would then not refresh the page, but show the hidden fields.
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

  6. #6
    JPC Addict
    Join Date
    Feb 2002
    Posts
    140
    Quote Originally Posted by Gwaihir View Post
    I meant someUrl as a variable, as I assumed you wanted to set the url dynamically. To put in a literal value like that, simply add quotes around it. Oh, and loose the space after .replace. I doubt all browsers support it.

    <a href="javascript:window.location.replace ('new_record.php?param=test');">refresh</a>
    The quotes were needed around the parameter, now the link functions. However, that javascript (javascript:window.location.replace) does not produce a page refresh (which I see below you do not think is needed). I would not need any javascript at all to send a parameter with a link and retrieve it, it was the page refresh via a link AND a new parameter I was after.

    BTW: something does happen when you try yours: you get a javascript error. I can recommend using Firefox + Firebug to help you make sense of those.
    Hmmm, I do not get a javascript error in the error console of Firefox 3.6.8., just a page refresh.

    That's regarding the original question. As for the bigger picture: this won't work well. If you refresh the page like that, the user will loose all the info he has entered but not yet submitted.
    I'm using $_SESSION variables to store everything and that part works fine. I'm thinking I should probably be able to also use $SESSION variables to solve my issue here...I'm just looking to keep it as simple as possible ("...but no simpler..." as Einstein said!)

    It would be much simpler to put all the fields in all the time, but hide those optional ones (display:none in your css). Your javascript link(s) would then not refresh the page, but show the hidden fields.
    Yes, hiding them with css may be a good solution. I'll try.

    I'm still curious in general if it's possible to have a link that refreshes a page AND sends a new parameter. The method (javascript, php $_SESSION variables, etc.) does not matter.

    Thanks for all of your suggestions.

  7. #7
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,562
    Quote Originally Posted by soundser View Post
    However, that javascript (javascript:window.location.replace) does not produce a page refresh
    Quote Originally Posted by soundser View Post
    that refreshes a page AND sends a new parameter.
    In what sense? What exactly does that mean to you?

    To me, to refresh a page, is to reload it, exactly the way it was initially loaded (same URL, same GET or POST, all the same parameters). That's also how the JavaScript function is named: reload().

    So, when you said you want to refresh a page AND send a new parameter, to me that meant you want to load the same page again, with one more parameter added to the URL.

    Hmmm, I do not get a javascript error in the error console of Firefox 3.6.8., just a page refresh.
    Well, I must admit that I'm at 3.6.4, but nevertheless, as first you confirmed it didn't refresh without the quotes, I think you're confusing some things.

    I'm using $_SESSION variables to store everything and that part works fine.
    That's server side and therefore doesn't include what wasn't submitted to the server yet.

    Anyway, go for the hiding and showing. Much more elegant .
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

  8. #8
    JPC Addict
    Join Date
    Feb 2002
    Posts
    140
    Quote Originally Posted by Gwaihir View Post
    In what sense? What exactly does that mean to you?

    To me, to refresh a page, is to reload it, exactly the way it was initially loaded (same URL, same GET or POST, all the same parameters). That's also how the JavaScript function is named: reload().
    In this case, the same POST, because the page on which the reload button would appear is a form, that itself came from a form.

    Quote Originally Posted by Gwaihir View Post
    So, when you said you want to refresh a page AND send a new parameter, to me that meant you want to load the same page again, with one more parameter added to the URL.
    Yes, or sent via POST maybe with a hidden field.

    Quote Originally Posted by Gwaihir View Post
    Well, I must admit that I'm at 3.6.4, but nevertheless, as first you confirmed it didn't refresh without the quotes, I think you're confusing some things.
    Without any parameters <a href="javascript:this.location.reload(); ">refresh</a> will reload the page, which seems to get me 1/2 the way there. I asked how to send a parameter with the link. Confusing things? No doubt...

    Quote Originally Posted by Gwaihir View Post
    That's server side and therefore doesn't include what wasn't submitted to the server yet.
    Yes, but it's only the info that has been submitted to the server (on the initial form) that needs to be stored.

    Quote Originally Posted by Gwaihir View Post
    Anyway, go for the hiding and showing. Much more elegant .
    Elegant yes, but to further complexify things (that's not a word!) the css needs a selector for each item to show/hide. The form allows the user to add a variable number of fields (task fields, in this case) to each record and on each of those task fields to choose whether or not to include dimensions (Length, Width, Height), all in the context of a form that enters a variable number of records. If it were working I'd be bragging...

    It's difficult to know how to define the css selectors before the record has been inserted.

  9. #9
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,562
    To complicate things is the word, I believe . That should not complicate your CSS though. I'd use just one class on all the hideable fields, say "hideable". It changes your JavaScript a bit: that shouldn't unhide all the hidden parts at once, but only those in the part where the button itself is placed. Personally, I'd bring in a library like jQuery or Prototype, because they make it simple to find such elements.

    As for the initial line of thinking: indeed, replace() won't help you, as it does a GET, not a POST. You could have your javascript submit the form, but you'd of course have to tell that apart from the "I'm done" submit action. If you're still pondering this line, google a bit for a form with two submit buttons.

    Best of success with it! (I'll leave it at this.)
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

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
  •