Jump to content

Preventing A Page Refresh From Js Function


Web Weenie

Recommended Posts

I have a Google search form on my web site, how do I prevent a page refresh when the user clicks submit when nothing is in the search field...Here's my code:

function searchWeb() {if (document.search.searchme.value =='Google Search...'){ alert("Sorry, nothing to search for... Please enter search criteria."); return false; }else{ results=window.open  (window.document.href="http://www.google.com/search?btnG=Google+Search&q="+document.search.searchme.value.replace(" ","+"));}}

I have a return false after the alert and a return false inline with the onclick that calls the js above.

Link to comment
Share on other sites

Can you show us the HTML that calls the JavaScript?

Link to comment
Share on other sites

Can you show us the HTML that calls the JavaScript?
Here it is...
<form id="subscribeform" action="" name="search" title="Perform A Google Web Search"><div id="g_search"><input name="searchme" type="text" size="10" value="Google Search..." onfocus="if(this.value=='Google Search...'){this.value=''};" onblur="if(this.value==''){this.value='Google Search...'}; return false;" id="mysearch" /><input type="submit" id="submit_button" value=" " size="80" onclick="searchWeb()";return false; /></div>

Any Ideas?

Link to comment
Share on other sites

It works fine for me. Watch the quotes in your onsubmit event. The return statement is part of the code to execute. I'm not sure what you're doing here though:results=window.open(window.document.href="..."))Are you trying to use window.open or are you trying to set the URL of the current page? The document object does not have an href property, you're probably looking for window.location.href instead (but that still wouldn't go inside a call to window.open).Also, instead of replacing spaces with plus signs, you can use the encodeURIComponent function to encode all invalid characters, not just spaces.

Link to comment
Share on other sites

It works fine for me. Watch the quotes in your onsubmit event. The return statement is part of the code to execute. I'm not sure what you're doing here though:results=window.open(window.document.href="..."))Are you trying to use window.open or are you trying to set the URL of the current page? The document object does not have an href property, you're probably looking for window.location.href instead (but that still wouldn't go inside a call to window.open).Also, instead of replacing spaces with plus signs, you can use the encodeURIComponent function to encode all invalid characters, not just spaces.
Not sure what you mean... Which quotes?I could be doing this wrong but I'm opening a new window and calling the Google url + the search criteria from the textfield. Or calling the alert box if the textfield is 'Google Search...'.the reload appends ?searchme=Google+Search... in the address bar. I'm thinking that the onblur is causing the problem. I'm not sure where that comes from because if I comment out the js to open a page it still happens.
Link to comment
Share on other sites

Not sure what you mean... Which quotes?
<input type="submit" id="submit_button" value=" " size="80" onclick="searchWeb()";return false; />--------------------------------------------------------------------------------^

P.S. that would have generated a validation error.

Link to comment
Share on other sites

<input type="submit" id="submit_button" value=" " size="80" onclick="searchWeb()";return false; />--------------------------------------------------------------------------------^

P.S. that would have generated a validation error.

hmmm, are you talking about the value=" " property? I'm doing some css shenanigans and have the visual submit button as part of a background image, I guess I could use a transparent gif. Or is there something else I'm not seeing. I have no problem with writing proper code or learning to write proper code. I'm just avoiding the validator. I looked for any reference but I couldn't find anything beyond a basic submit statement. Although I don't think this has anything to do with my refresh issue which I'm probably going to have to live with.
Link to comment
Share on other sites

It's the closing quotation mark on the onclick attribute! :) And it is directly linked to the refresh issue (assuming the code was the same when you moved it to the form tag), because having the quotation mark in the place you have means the the return is never executed. I mentioned the validator because if you had used it as part of your debugging process, then you would have easily seen the problem. And proper code is valid code, so you have nothing to fear.onclick="searchWeb()";return false;P.S. the empty value attribute is fine.

Link to comment
Share on other sites

It's the closing quotation mark on the onclick attribute! :) And it is directly linked to the refresh issue (assuming the code was the same when you moved it to the form tag), because having the quotation mark in the place you have means the the return is never executed. I mentioned the validator because if you had used it as part of your debugging process, then you would have easily seen the problem. And proper code is valid code, so you have nothing to fear.onclick="searchWeb()";return false;P.S. the empty value attribute is fine.
Ooops! looked at that 1000 times and never saw that...Ok you guys are beating me up about validating... you're all right. I get kicked in the balls by the validator and punched in the nose by you guys... I'm covering my nuts with one hand and my nose with the other, I'm now coding with my toes... :-)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...