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.
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.
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.
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
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
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
The asp.net Button is more suitable for such tasks.
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 :)
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
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
is working on a reply...