0

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.

2
  • One option is: window.onload = exludeUserAgent; Commented Sep 7, 2015 at 10:11
  • Are you wanting to exclude functionality based on a specific feature? If so, I'd suggest feature detection instead of User agent sniffing. Commented Sep 7, 2015 at 10:24

5 Answers 5

2

Write the below code in your JSP page:

<script type="text/javascript">
$(document).ready(function(){
   exludeUserAgent()
});
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

The question has no jquery tag, so your answer seems to be irrelevant
0

Just use this single block in your webpage:

<script type="text/javascript"> exludeUserAgent();</script> 

Comments

0

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

Comments

0

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.

Comments

0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.