Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have created 1 master page and 4 child pages.
I have written a javascript function for childpages 1 and 2.
Childpages 3 and 4 do not contain any javascript code.
I have provided linkbuttons in master page for navigate childpages.

The javascript function name is charscheck(); it checks the characters of a textbox.
This function name is written in the body tag of the master page.
(i.e., < body onload="charscheck()" >).

My problem is that when I click linkbuttons of childpages 1 and 2 to redirect to those pages, it's working, but when I am click linkbuttons of childpages 3 and 4 it displays a script error.
(i.e. "Microsoft JScript runtime error: Object expected").

That says that childpages 3 and 4 do not contain the javascript function.
If i write the javascript function for each and all of the pages then the error message is not displayed.
But in our project javascript is used only childpages 1 and 2.

How do I solve this problem?

My aim is to go to childpage 3 and 4 directly and not to display the javascript error message.

How can this be done?

Thanks.
Posted
Updated 18-May-10 1:53am
v2
Comments
Dalek Dave 18-May-10 7:53am    
Edited for Grammar, syntax, readability and clarity of question.

Obiously, you'll get error, you called your js function from masterpage but the definition is not available child3 and child4 so it will through error. So now you have two options
- Either remove calling the function from body's tag onload function and call the function from child pages itself.
- Or provide the definition in all child pages but dont do anything on child3 and child4 page.
 
Share this answer
 
Put the Javascript function 'charscheck()' in your Master page. You have the call to this function in Master page, so this will remove your error.

Now added benefits: It's in one place and maintainbility is easy. If any change is needed in future, just do it in one place. If Page3/4 needs the same thing in future, you dont need to do anything, just use it!
 
Share this answer
 
if you don't want to include the javascript in everypage you can add the onload method for just the pages that require it.

set the body to runat="server" and give it an id.



You will then be able to access it server side.

In the server side code for your first 2 pages add:

elBody.Attribute.Add("onload", "charscheck");
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900