Javascript to generate new XHTML content not working - javascript

I've been scratching at my head with this for a few days now and I cannot find any solution to this online or even with a professor.
I'm working on a system that requires some pages to be capable of dynamically generating new HTML content whilst running (JSF web project running on glassfish4 server)
I've found some resources on how to do this but it only worked when running on a standard HTML page. When I convert it to the XHTML standard the function no longer generates anything (although it is still being called, the alert box confirmed that).
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=ISO-8859-1"></meta>
<title>Insert title here</title>
</head>
<body>
<SCRIPT type="text/javascript">
function addQuestion() {
alert('yay');
var exam = document.getElementById('divarea');
var temp = document.createElementNS('h', 'button');
temp.setAttribute('value', 'child');
exam.appendChild(temp);
}
</SCRIPT>
<div id="divarea">
</div>
<h:commandButton value="create new stuff" onclick="addQuestion()"/>
</body>
</html>
If anyone can shed some light on this issue it'd be greatly appreciated.
P.S.
Here are the resources I've been using to attempt troubleshooting.
Converting javascript to work with XHTML
Creating elements dynamically using javascript

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=ISO-8859-1"></meta>
<title>Insert title here</title>
</head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<body>
<SCRIPT type="text/javascript">
function addQuestion() {
var oldcon=$("#divarea").html();
var newcon='<button>child</button>';
var updcon=oldcon+newcon
console.log(newcon);
$("#divarea").html(updcon);
}
</SCRIPT>
<div id="divarea">
</div>
<button onclick="addQuestion()"/>dff</button>
</body>
</html>

Related

Can't manage to update raspberry pi, gpio-header html file

I'm very new to programming with webiopi and very new to the raspberry pi in general.
Me and my brother are planning to build a robot running on a raspbery pi. We're planning to build a camera on the robot. We would stream the camera through our raspberry pi and controll the robot, both through the same website.
However, here's my problem: I downloaded and installed webiopi for my raspberry pi. And with that webiopi comes an example code which he will execute. These are simple html files. If i make any change to them the raspberry pi just shows the original file.
Here's the first original:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content = "width = 420, user-scalable = no" />
<title>Webiopi</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
webiopi().ready(function() {
webiopi().RPiHeader().createTable("content");
w().refreshGPIO(true);
});
</script>
</head>
<body>
<div id="content" align="center"></div>
</body>
</html>
Here is the version I want to upload(for a test):
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content = "width = 420, user-scalable = no" />
<title>Webiopi</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
webiopi().ready(function() {
webiopi().RPiHeader().createTable("content");
w().refreshGPIO(true);
});
</script>
</head>
<body>
<P>Testesttestest</p>
<div id="content" align="center">
<img width="300" height="240" src="http://raspberrypi:8080/?action=strem"><br/>
</div>
</body>
</html>
I don't know if any of you guys ever had this problem. But I can't continue my project if i can't update my webpage.
Thanks for u're help
Justin
I think you're not editing the right files.
Webiopi installs a server on your Raspi. From a fresh install, files are stored in /usr/share/webiopi/htdocs/.
Also, you should check this page about configuration file in order to modify the default folder the files are stored in, and other usefull config features.

Window opener problems IE11?

For some reason the following code (which triggers parent-page functions) will not work in IE11. Its been working fine uptil now but no longer. To note: I have tried to turn off Protected mode (which is not a solution for customers) but this didn't work either!
<!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="X-UA-Compatible" content="IE=8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script type="text/javascript">
var accounts = window.opener.$('input[name="accounts_added"]');
var text = window.opener.$('#counter_$id');
$(accounts).val(parseInt($(accounts).val()) + 1);
//window.opener.$('input[name="subBtn"]').show();
window.opener.updateAjaxbox();
window.close();
</script>
</body>

How to access javascript bookmarklet in firefox in javascript

I have made javascript bookmarklet for firefox. Now i want to access it in javascript like other document/ DOM elements are accessed through their id or class. Please anybody tell me how i can access my javascript bookmarklet . Actually i want to make it blink /change it color by accessing it.
My bookmarklet code
<!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" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<title>Untitled Document</title>
<script>
</script>
</head>
<body>
<div id="toolbar" >
<a id='button' href='javascript:alert("my bookmarklet");'
>Lysted</a>
</div>
</body>
</html>
Now i want to access bookmarklet in "script tags/ javascript". Please please help anybody

How do I make this JavaScript code portable?

The following code works only in Chrome. I need it to run on all major browsers. Firefox and IE load the image once. Opera doesn't load it at all.
The screen.jpg file is sent with Cache-Control: no-cache, no-store. I cannot use a library like jQuery because they're too big.
<!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></title>
<script type="text/javascript">
function refresh()
{
document.getElementById("screen").src = "screen.jpg";
setTimeout("refresh()", 500);
}
</script>
</head>
<body style="margin:0" onload="refresh()">
<img id="screen" style="width:100%"/>
</body>
</html>
you can append random number to the end of source so that the image is re-requested every time:
document.getElementById("screen").src = "screen.jpg?" + Math.random()*999999;

Getting Render Page Source with Javascript

I have following 2 files as below:
<!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>
</head>
<script>
function init(){
document.getElementById("mytest").innerHTML= "Results after rendering...";
}
</script>
<body onload="init();"><div id="mytest">OK</div>
</body>
</html>
The second page usually give the alert popup all source code of first page.
<!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>
</head>
<script>
xmlhttp.open("GET", "test.html",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
alert(xmlhttp.responseText)
}
}
xmlhttp.send(null)
</script>
<body>
</body>
</html>
All I want to do is I would like to get ONLY source code after rendering. How can get all code after rendering instead of getting all original code. So i can read < div id="mytest">Results after rendering...< / div> when I try with XMLHTTP. How can I do how to get the code which are already render for page, I want only with classic Javascript or DOM, I don't want with Jquery, JSON, Mootool at all. thanks in advance.
Instead of loading the page with ajax, use your browser's iframe support to your advantage.
Change the second file that alerts the HTML source to something like this:
<!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>
</head>
<script>
function displayAlert()
{
alert(document.getElementById('iframe').contentDocument.body.innerHTML);
}
</script>
<body onload="displayAlert()">
<iframe src="test.html" id="iframe" style="display:none;"></iframe>
</body>
</html>
This will load test.html in an invisible iframe. Your browser will automatically render test.html inside the iframe and will call displayAlert() when it is done. displayAlert() will grab the the source code inside the iframe and alert it. However, this solution will only work if test.html is on the same server as the script above. If test.html is on a completely different server, this solution will not work because the permission to access the iframe will be denied. If this is the case, I can let you know of another solution that will bypass this.
I'm not sure I know what you're really asking, but why not just get:
alert(document.body.innerHTML);
to get the actual rendered body HTML with any changes that have been made by scripts upon loading.
Note: even unmodified parts of innerHTML will not always compare exactly to the original source HTML because in some cases browsers are reconstituting the innerHTML from some other parsed data form so attributes may not have the same quoting or be in the same order and capitalization may not be the same.

Categories

Resources