Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • tla 35 posts 71 karma points
    Jul 09, 2009 @ 14:47
    tla
    0

    Javascript redirect

    Greetings,

     

    I have a problem getting a javascript redirect to work correct.I'm not sure which forum section to post this on, so trying here.

     

    On the umbraco site masterpage, I have a textbox and a button. If the textbox has any value (ie. is not empty) when clicking the button, the page should redirect to the search page (link to document is Link to document /search.aspx).  At least that's how it was thought to work - below is the button.

    <input src="/images/site/bg_submitNormal.gif" class="btnSubmit" onclick="javascript: var param = document.getElementById('searchField').value; if (param.length != 0 && param != 'Søg') {  window.location = '/soeg.aspx?search=' + param; }" value="Søg" type="image">

     

    while applying standard javascript debugging procedure (yes, I'm talking about everybody's favorite, the alert()), I stumbled over something interesting. Putting an alert after the window.location will cause the redirect (like so: {  window.location = '/soeg.aspx?search=' + param;  alert(param); }), but not if it is before the window.location.

     

    The window.location syntax itself work (we use it further up on the page if the request is for a specfic page and it redirects just fine). Another thing to notice is, that when the butting is clicked, the page url is shown as .aspx?. I do not know if this has any meaning.

     

    cheers

  • Petr Snobelt 923 posts 1535 karma points
    Jul 09, 2009 @ 16:41
    Petr Snobelt
    100

    try add return false; at end of your script or use <img> tag instead of input. But Peoples without javascript can't use your search ...

    You should add your search button into form and handle logic in form's onSubmit method

    Petr

  • Vladimir Dobrov 45 posts 167 karma points
    Jul 09, 2009 @ 18:07
    Vladimir Dobrov
    1

    The asp.net Button is more suitable for such tasks.

  • tla 35 posts 71 karma points
    Jul 10, 2009 @ 08:08
    tla
    0

    returning false did the trick, thanks! 

    I am a .net person more than a javascript person, so I would take the .net button approach myself, but I merely got the job of fixing it:)

     

    I do agree that relying on users to have javascript enabled can be a problem and it is not something I would do if I could just write a c# user controll, it did, however, interest me, why inserting adding the alert caused the redirect to happen and not without.

     

    Again, thanks heaps guys :)

  • Petr Snobelt 923 posts 1535 karma points
    Jul 10, 2009 @ 16:10
    Petr Snobelt
    1

    If you do not add alert, or return false, then your form is submitted to server (default behavior of input type=submit or image). If you add alert, browser pause submitting form before you click on ok. And JS code can run and redirect.

    but you should rewrite your code to something like this

    <form action="/soag.aspx" onSubmit="if (document.getElementById('search').value.length != 0) return true;return false;">
     <input type="text" id="search" name="search" />
     <input src="/images/site/bg_submitNormal.gif" class="btnSubmit" type="image" />
    </form>

    This way if anybody have not js enabled form is still submitted to server - to search url. But it can't be in another form tag.

     

    Petr

     

Please Sign in or register to post replies

Write your reply to:

Draft