Connecting Tech Pros Worldwide Help | Site Map

javascript functions not working on asp.net page

Familiar Sight
 
Join Date: Jul 2007
Posts: 180
#1: Jan 15 '08
I have a page that contain input form, a contact us page. I also have a few javascript functions, one in particular sends the visitor back to the home page.

My javascript is not functioning properly, the user is supposed to be directed back to the home page, but instead the page simply seems to reload. I would assume that the form is trying to post back, however, I would like to avoid this if possible. In the past for other asp.net button, I could add:

if (!Page.IsPostBack)
{
code here...
}

Is there a similar work around for javascript functions?
Familiar Sight
 
Join Date: Jul 2007
Posts: 180
#2: Jan 15 '08

re: javascript functions not working on asp.net page


I suppose there could be a different issue here. Has anyone at least heard of this type of thing happening previously. To simplify:

-1 .aspx page
-1 contact us form with submit button that initiates method of sending the user input as an email
-1 HTML input of type image that when click is supposed to initiate javascript funtion home(), but does not work properly and instead seems to reload the page.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: Jan 16 '08

re: javascript functions not working on asp.net page


Show your JavaScript code.
Familiar Sight
 
Join Date: Jul 2007
Posts: 180
#4: Jan 16 '08

re: javascript functions not working on asp.net page


Quote:

Originally Posted by acoder

Show your JavaScript code.

Brace yourself,

function navURL(url) {
window.location.href = url;
}
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: Jan 16 '08

re: javascript functions not working on asp.net page


OK, so how do you call this code?
Familiar Sight
 
Join Date: Jul 2007
Posts: 180
#6: Jan 16 '08

re: javascript functions not working on asp.net page


Quote:

Originally Posted by acoder

OK, so how do you call this code?

<input type=image src="Images/QuikeyGreenLogoOnGREENfield.gif" onClick="navURL('http://www.physicaladvertising.com');return false;" />

Previously I did not have return false;. The function now operates properly.

On another note, on this same page, when the submit button is clicked, a c# method is called, I would like to call a different javascript function from that method if possible. Essentially the c# method sends the contents of the contact us page in an email, upon doing so I would like to call a javascript function that grays out the background and makes a div tag present. Can javascript be called from server side controls?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Jan 16 '08

re: javascript functions not working on asp.net page


Quote:

Originally Posted by mcfly1204

Previously I did not have return false;. The function now operates properly.

Yes, that should solve it and has.
Quote:

Originally Posted by mcfly1204

On another note, on this same page, when the submit button is clicked, a c# method is called, I would like to call a different javascript function from that method if possible. Essentially the c# method sends the contents of the contact us page in an email, upon doing so I would like to call a javascript function that grays out the background and makes a div tag present. Can javascript be called from server side controls?

Just output the JavaScript from the server-side code to be run when the page loads.
Reply