Reputation: 42957
I am pretty new in JavaScript and I have the following doubt.
Into a JSP page I include a .js file that contains a function definition, in this way:
<script src="<c:url value="resources/js/userAgentInfo.js" />"></script>
Into this userAgentInfo.js file I have define a function, something like this:
function exludeUserAgent() {
...............................................
...............................................
...............................................
if (browserName === "Microsoft Internet Explorer" && majorVersion <= 10) {
alert("EXCLUDE");
return true;
}
return false;
}
OK, now my problem is: how can I call and perform this exludeUserAgent() function into my page? I have included the file that contain its definition but now I want to perform it when the page is loaded.
Upvotes: 0
Views: 59
Reputation: 24590
Wy to wait until the page load? I think you want to do that immediately when your script file loads.
Just add call to the function on the script file:
exludeUserAgent()
function exludeUserAgent() {
...............................................
...............................................
...............................................
if (browserName === "Microsoft Internet Explorer" && majorVersion <= 10) {
alert("EXCLUDE");
return true;
}
return false;
}
One more note: As you can see you can call the function before you defined it. This is how JavaScript works...
Upvotes: 0
Reputation: 13089
Typically, I attach an event to the onload
event of the window. This will be fired whenever all the resources initially present on the page have loaded (css/html/images/sounds/videos).
To do this, you simply need do the following:
window.addEventListener('load', onDocLoaded, false);
Next, you need a function that will actually handle this event:
function onDocLoaded(evt)
{
/* initialization code goes here */
}
In your case, you'd just need to add a call to the exludeUserAgent
function to the body of onDocLoaded
.
Upvotes: 0
Reputation: 433
if you have jQuery in your page try:
<script type="text/javascript">
$(document).ready(function(){
exludeUserAgent()
});
</script>
if no jQuery is available you can try:
<script type="text/javascript">
window.onload = function(){
exludeUserAgent();
};
</script>
both script blocks just need to be part of your jsp
Upvotes: 0
Reputation: 3572
Just use this single block in your webpage:
<script type="text/javascript"> exludeUserAgent();</script>
Upvotes: 0
Reputation: 98
Write the below code in your JSP page:
<script type="text/javascript">
$(document).ready(function(){
exludeUserAgent()
});
</script>
Upvotes: 2