I am new to Javascript and Java Server Faces and I am having the next Problem.
The Code below is working ok:
<?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:ui="http://java.sun.com/jsf/facelets">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript">
function initialize()
{
alert("Testing");
}
</script>
</head>
<body onload="initialize()">
<h1 align="center">Sol-Tech</h1><br />
</body>
</html>
But when I add a FOR loop, it doesn't work:
<?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:ui="http://java.sun.com/jsf/facelets">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript">
function initialize()
{
for(var i; i<3; i++)
{
alert("Test");
}
}
</script>
</head>
<body onload="initialize()">
<h1 align="center">Sol-Tech</h1><br />
</body>
</html>
Has anyone any suggestion on how to perform a FOR loop in javascript without getting an error?
Thanks in advance,
Emanuel
You're using an outdated version of Mojarra which exposes a bug wherein this kind of IllegalArgumentException: null source is incorrectly been thrown when the view file contains a XML syntax error or when the view couldn't be restored. See also java.lang.IllegalArgumentException: null source and JSF issue 1762.
If you upgrade to a newer Mojarra version (currently already 2.1.14), then you'll in this particular case get a more self-explaining XML syntax error on the character < which indicates the start of a XML element. Facelets is namely a XML based view technology and parsed by a SAX parser. You'd need to replace the XML special character < by <
for(var i=0; i<3; i++) {
alert("Test");
}
or to put the whole script in a CDATA block
<script type="text/javascript">
<![CDATA[
function initialize()
{
for(var i=0; i<3; i++)
{
alert("Test");
}
}
]]>
</script>
or to just put it in its own .js file so that it won't be parsed as XML.
<script type="text/javascript" src="script.js"></script>
See also:
Writing JavaScript for XHTML
Unrelated to the concrete problem, the JS syntax error (uninitialized var i which I've already fixed in code snippets) is actually a completely different problem and would only show an error in client side in browser's JS console and definitely not cause an exception in the server side as JS doesn't run in webserver at all, but only in webbrowser.
Related
Good day,
I am working on a c# web application, everything is working until I add in a normal JavaScript.
The html code is something as follow:
<!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">
<head runat="server">
<title></title>
<script src="scripts/JScript.js" type="text/javascript"></script>
<asp:ContentPlaceHolder id="headContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<asp:ContentPlaceHolder id="title" runat="server"></asp:ContentPlaceHolder>
<asp:ContentPlaceHolder id="bodyContent" runat="server"></asp:ContentPlaceHolder>
<!-- some code here -->
</body>
The JScript.js is the JavaScript that put in.
The JavaScript code is something as follow:
function getCookie(catId) {
var ebRand = Math.random() + '';
ebRand = ebRand * 1000000;
document.write('<scr' + 'ipt src="HTTP://bs.serving-sys.com/Serving/ActivityServer.bs?cn=as&ActivityID=553971&rnd=' + ebRand + '"></scr' + 'ipt>');
}
This JavaScript function will be trigger when a link button is click.
I hit error in IE but Chrome and Mozilla is working fine.
My error in IE console is
HTML1527: DOCTYPE expected. The shortest valid doctype is "<!DOCTYPE html>".
After search in Google, I try to put in
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
as new DOCTYPE, but it not working also.
and I have try put
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
inside <head></head>
But I still get the same error in IE also.
Is is problem in ContentPlaceHolder?
Other than that, I have go to W3C Markup Validation Service to do some validation of the address :
http://validator.w3.org/check?uri=HTTP%3A%2F%2Fbs.serving-sys.com%2FServing%2FActivityServer.bs%3Fcn%3Das%26amp%3BActivityID%3D553971%26amp%3Brnd%3D20079.082210606393&charset=%28detect+automatically%29&doctype=Inline&group=0
And go to http://validator.w3.org/check to put in <script src="HTTP://bs.serving-sys.com/Serving/ActivityServer.bs?cn=as&ActivityID=553971&rnd=20079.082210606393"></script>
as well.
But not understand what is the validation result means.
Kindly advise.
IE issues a warning about <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">, because such a string is not a valid doctype according to HTML5. The warning as such has no impact on anything. It’s just part of HTML5 evangelism.
After you changed it to <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">, the issue was removed. But you probably saw the results of using some cached copy that has a wrong doctype. Testing with a fresh file (with a new name) should fix this issue.
Of course, the code as such does not validate, since the ASP tags are taken literally and interpreted as body content, but that’s a different issue.
The practical move is to use <!DOCTYPE html> as suggested. If you wish to still validate against the XHTML 1.0 specification for example, you can do this using the validator’s user interface to override the doctype.
But it’s not a big issue; you can just as well simply ignore the warning. If you have some functional errors, they are caused by something else and should be asked separately, with sufficient data provided.
I am trying to load data from an api using ajax and json however it keeps giving this error.
ReferenceError: Can't find variable: $
Global code
This is the html code used
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script src="test.js"></script>
</head>
<body>
<div id="div1">Old Content</div>
<button>Load</button>
</body>
</html>
Below is the jQuery Code
$(document).ready(function(){
$("button").click(function(){
var url = 'http://creative.coventry.ac.uk/~bookshop/v1.1/index.php/genre/list';
$.get(url, function(data,status){
console.log(data);
});
});
});
The "$" keyword is used by the jQuery library, which you haven't included in your HTML to be loaded alongside the rest of the document.
Add this before your existing script tag to load jQuery from Google's CDN:
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
Note that you might run into problems trying to retrieve cross domain json files. Consider looking up jsonp if that's the case.
Edit: fixed the URL
I have found the thread answered by BalusC
It describes the exact problem I have. When I found it I thought I have found my answer as well, but for me the solution is not working. I had problems with the < h:outputScript> tag already, maybe it is the same problem (the < script> tag is working).
I have the same project description as the user Plaha, that put the question. I am trying to call a JavaScript function defined in the template page with the parameters attributes of the backing JSF bean. But for now the alert function is enough.
I`ve tried to make the page gridTest.xhtml to look like content.xhtml from the example, but it doesn't work.
The gridsTemplate.xhtml looks exactly like the template in the thread + I have the < head> tag in which I have some JavaScript functions. My gridTest.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<link rel="stylesheet" type="text/css"
href="/juritest/resources/css/style.css" />
<script type="text/javascript"
src="/juritest/resources/js/jquery-1.8.2.js"></script>
<script type="text/javascript" src="/juritest/resources/js/menu.js"></script>
</h:head>
<body>
<ui:composition template="../../templates/gridsTemplate.xhtml">
<ui:define name="content">
<h:form rendered="#{gridPopUpBean.testNotStarted}">
...
</h:form>
<h:form rendered="#{not gridPopUpBean.testNotStarted}">
<h:outputScript target="body">
alert("working?");
</h:outputScript>
...
I have tried for curiosity to put the < h:outputScript directly under the ui:define like in the example, but it doesn't work either.
I've recently ran into a problem and I would really use a bit of help from the community. I'm trying to load a HTML file using jQuery load().
The index file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Question</title>
<meta http-equiv="Content-Type" Content="text/html; charset=euc-kr" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function restart(){
$("#wrap").load('new-content.html').hide().fadeIn('slow');
}
</script>
</head>
<body>
<div id="wrap">
<div id="content">Text before restart: 남성</div>
</div>
<br />
Restart
</body>
</html>
The new HTML file includes some simple HTML content and a call to a .js file (used for some animations). The new content file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Question</title>
<meta http-equiv="Content-Type" Content="text/html; charset=euc-kr" />
<script src="main.js" language="javascript" type="text/javascript" />
</head>
<body>
<div id="contentInner">Text after restart: 여성</div>
</body>
</html>
As you can see I've included the correct encoding charsets (for both files) and I've double checked and I can confirm that every file used for this part of the code it's encoded as "KOREAN" (checked in different apps like Dreamweaver, Notepad++, Sublime Text 2).
Yet still once I load the content the characters are not encoded properly; question marks appearing instead of characters.
Of course I made some searching before posting and I found a few helpful topics about this called the PHP header solution, but this isn't an options since it need to use HTML/JS/CSS only.
The same behavior occurs when I'm using the Japanese language too. In other latin based languages everything is working perfectly fine of course.
Any sort of input/advice/help would be much much appreciated.
Thank you!
The w3c validator service complains that the following html is invalid. It does not like the ampersand(&) in my javascript. But ampersands are allowed in javascript strings, aren't they?
<!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">
<head>
<title>Page Title</title>
</head>
<body>
<script type="text/javascript">
function search(query) {
redir = "http://search.mysite.com/search?s=FIN&ref=&q=" + query;
window.location.href = redir
return false;
}
</script>
<span>This is all valid HTML</span>
</body>
</html>
All browsers will take this, but to make it valid X(HT)ML you need to put the Javascript code in a CDATA block.
Even in javascript w3c validator don't like ampersands. Try to comment your javascript from validator
<!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">
<head>
<title>Page Title</title>
</head>
<body>
<script type="text/javascript">
//<![CDATA[//><!--
function search(query) {
redir = "http://search.mysite.com/search?s=FIN&ref=&q=" + query;
window.location.href = redir
return false;
}
//--><!]]>
</script>
<span>This is all valid HTML</span>
</body>
</html>
No, it is indeed not valid. If you want to use in-line JavaScript in an XHTML file, you'll need to wrap the JavaScript in CDATA. If you don't want to do that, then you're stuck with encoding &, < and >, which in JavaScript can be quite a pain.