1

I am newbie to jQuery.

In netbeans I have jquery-1.7.2.js where all HTML files are present. I am creating JSF 2.0 project.

Below is the code I have

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:t="http://myfaces.apache.org/tomahawk"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
    <h:head>
        <title>Welcome to MySite.</title>
    </h:head>

    <script language="text/javascript" src="#{facesContext.externalContext.requestContextPath}/faces/jquery-1.7.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            alert('I am here...');
            $("#table tr:last").after("<tr><td>some</td><td>content</td></tr>");
        });

    </script>

I believe with this code, whenever document gets loaded, I should have got alert as Hi, I am here... however I am not getting any alert.

Also, when I see view source & click on jquery-1.7.2.js, it gets opened.

Any idea why I am not getting alert?

Update 1

I am following tutorial provided here, still no luck. :(

3
  • in order to identify the problem , try using <script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> Commented Jul 4, 2012 at 17:50
  • You might have checked it but still want to confirm once. You have mentioned that in the jsp source code when you click on jquery js file it is opened but does it have jquery code or its error report? Commented Jul 4, 2012 at 17:52
  • @Daniel : its working with your option... then what is the problem?? is jquery file is not good? Commented Jul 4, 2012 at 17:54

3 Answers 3

1

It seems like a bad location of file, I dont where exactly you placed the jquery file in your web app , but anyway It should be located in folder under the resources folder, So...

Add resources folder under the WebContent

and inside resources create js folder

then access the files like this

 <h:outputScript name="js/jquery-1.7.2.js" target="head" />
Sign up to request clarification or add additional context in comments.

5 Comments

Whilst this may work, this is the wrong usage of the library attribute. Get rid of it and use name="js/jquery-1.7.2.js".
thanks for help. just curious, why I have to do like this? I believe in my earlier projects I don't need to do anything like this...
It was a comment to Daniel's answer not to your question.
@BalusC , why is using library is wrong ? Any chance for a reference that explains that, cause till now I only saw its being used that way (with library) , for example mkyong.com/jsf2/resources-library-in-jsf-2-0
The library should represent the resource library, e.g. "primefaces", "omnifaces", "javax.faces", "mylibrary", etc, not the resource type like "js", "css", etc. Otherwise advanced resource library management in more complex JSF webapps will fail.
-2

I believe that JSF may be enforcing XHTML standards which tell it to ignore your script tag because it is outisde head or body tags. Have you checked whether your script tag is being rendered at all?

I have learned the hard way that the best way to do stuff when dealing with JSF is to do it the JSF way. In this case, it would be using h:outputScript tag.

Also, a good practice with JavaScript is to put it in a separate .js file even if you are going to use it on only one page. Makes it much easier to debug in a browser.

4 Comments

No, this is not the cause of his problem at all (experiment yourself first). Check the script URL closely once again to understand the cause of his problem.
He did say that jQuery is getting correctly loaded by the browser: "Also, when I see view source & click on jquery-1.7.2.js, it gets opened."
I know. Check the script URL once again. What doesn't belong there?
Not a JSF expert, but I'm guessing JSF won't evaluate expression in a plain html tag attribute?
-2

Are you sure about the line: "#{facesContext.externalContext.requestContextPath}/faces/jquery-1.7.2.js" ? If I were you, I would look at the browser Url, then code with a relative path w/r to that Url to point to jquery-1.7.2.js. Another possible debug way would be to make a copy of the jquery-1.7.2.js in the current folder (i.e. of the current .xhtml), then the script tag line would be src="./jquery-1.7.2.js". If you can see the alert, then the problem is from that line path.

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.