After load event for iframe? - javascript

I'm using an iframe as target for one of my forms. I need to transform the html of the iframe once the form is submitted. I tried using the onload event, but when the event is fired, my iframe does not have the html resulting from my form yet (right on the debugger breakpoint):
<html>
<head>
<title>Test</title>
</head>
<body>
<script language="javascript">
function loaded(){
var lResponse = document.getElementById('results');
debugger;
}
</script>
<div>
<table>
<tr>
<td width="100%">
<form id="serverForm" target="results" <web:taskProcessorName attribute="action" type="admin"/>>
<div>
<table>
<tr>
<td>
<div name="resultsDiv" id="resultsDiv" style="display: none">
<iframe name="results" id="results" onload="loaded" frameborder="0"></iframe>
</div>
</td>
</tr>
</table>
</div>
<!-- Hidden Inputs -->
<input type="hidden" name="server" value="..." />
</form>
</td>
</tr>
</table>
</div>
<script language="javascript">
document.getElementById('serverForm').submit();
document.getElementById('resultsDiv').style.display='block';
</script>
</body>
</html>
No 3rd party library - pure javascript.

I use the following instead and that seems to catch the event when the iframe html is set already:
function lLoadFunc(){
var lResponse = document.getElementById('results');
}
if(lResponse.addEventListener)
lResponse.addEventListener('load', lLoadFunc, true);

Related

tip display script cannot reset to default content

I was able to display a block of text in a td after mouseover event, however I aim to reset, i.e display oringinal content of the td on mouseout/mouseleave the code i used is as follow. pls help
I get an undefined error when the code is run. i think the problem is seeting the event to element variable.
<html>
<head>
<title></title>
<script>
<!--
var content=new Array()
content[0]='Menus first'
content[1]='Menu Second'
content[2]='Menu Third'
function changetext(whichcontent){
document.getElementById("descriptions").innerHTML=whichcontent
}
function reset(){
if (!scriptmenu.contains(event.toElement))
descriptions.innerHTML=temphtml;
}
//-->
</script>
</head>
<body>
<table width="400" border="1">
<tbody>
<tr>
<td><div id="scriptmenu" onmouseleave="reset()">
<p><a href="index.html" onMouseover="changetext(content[0])" >Menu First</a><br>
Menu Second<br>
Menu Third<br>
</p>
</div></td>
<td><div id="descriptions" align="left">
<p><b>Welcome</b><br>
Enjoy!<br>
</div></td>
</tr>
</tbody>
</table>
<script language="JavaScript1.2">
if (document.all)
var temphtml=descriptions.innerHTML;
</script>
</body>
</html>
Try this example. add onmouseout with every onmouseover
Other change to focus on
var defaultData = document.getElementById("descriptions").innerHTML;
function reset(){
if (!scriptmenu.contains(event.toElement))
descriptions.innerHTML=defaultData;
}
<html>
<head>
<title></title>
<script>
<!--
var content=new Array()
content[0]='Menus first'
content[1]='Menu Second'
content[2]='Menu Third'
function changetext(whichcontent){
document.getElementById("descriptions").innerHTML=whichcontent
}
//-->
</script>
</head>
<body>
<table width="400" border="1">
<tbody>
<tr>
<td><div id="scriptmenu" >
<p>Menu First<br>
Menu Second<br>
Menu Third<br>
</p>
</div></td>
<td><div id="descriptions" align="left">
<p><b>Welcome</b><br>
Enjoy!<br>
</div></td>
</tr>
</tbody>
</table>
<script language="JavaScript1.2">
var defaultData = document.getElementById("descriptions").innerHTML;
function reset(){
if (!scriptmenu.contains(event.toElement))
descriptions.innerHTML=defaultData;
}
</script>
</body>
</html>

Javascript won't run on my HTML page

I have modified this javascript code that I found on jsfiddle. My aim is to display tables when a link has been clicked, and when clicking on a different link it displays a different table and hide the previous table. Here's my code:
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled Document</title>
<script>
var elem=document.getElementById("loginTable");
var hide = elem.style.display =="none";
var elem2=document.getElementById("loginTable2");
var hide2 = elem2.style.display == "none";
function toggleTable(){
if (hide){
elem.style.display="table";
elem2.style.display="none";
}
else{
elem.style.display="none";
}
}
function toggleTable2(){
if (hide2){
elem2.style.display="table";
elem.style.display="none";
}
else{
elem2.style.display="none";
}
}
</script>
</head>
<body>
<a href="#" id="loginLink" onclick="toggleTable();return false" >Business Cards</a>
<table id="loginTable" border="1" align="center" style="display:none">
<tr>
<td>
Business card table
</td>
</tr>
</table>
<a href="#" id="loginLink" onclick="toggleTable2();return false" >Letterheads</a>
<table id="loginTable2" border="1" align="center" style="display:none">
<tr>
<td>
Letterhead table
</td>
</tr>
</table>
</body>
</html>
It worked on jsfiddle, but when I place it on my HTML page, it won't run.
Any suggestions?
Also, I have no knowledge of JavaScript, I have only modified it and it ran on jsfiddle, but not my HTML page.
when your script load its not find DOM elements so its not working
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<a href="#" id="loginLink" onclick="toggleTable();return false" >Business Cards</a>
<table id="loginTable" border="1" align="center" style="display:none">
<tr>
<td>
Business card table
</td>
</tr>
</table>
<a href="#" id="loginLink" onclick="toggleTable2();return false" >Letterheads</a>
<table id="loginTable2" border="1" align="center" style="display:none">
<tr>
<td>
Letterhead table
</td>
</tr>
</table>
<script>
var elem=document.getElementById("loginTable");
var hide = elem.style.display =="none";
var elem2=document.getElementById("loginTable2");
var hide2 = elem2.style.display == "none";
function toggleTable(){
if (hide){
elem.style.display="table";
elem2.style.display="none";
}
else{
elem.style.display="none";
}
}
function toggleTable2(){
if (hide2){
elem2.style.display="table";
elem.style.display="none";
}
else{
elem2.style.display="none";
}
}
</script>
</body>
</html>
if you want to use script in head section you need to write window.onload function
<script>
window.onload=function(){
// your JS code
}
</script>

Java script function not called

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
</head>
<br><br><br><br>
<body style="background-color: cornflowerblue" >
<form action="Login" method="post" name="frmLogin">
<table style="background-color: darkgrey" align="center" border="1">
<thead>
<tr>
<th colspan="2" style="font-size: xx-large;color:darkmagenta" >Login Details</th>
</tr>
</thead>
<tbody>
<tr>
<td style="font-weight: bold" >Username</td>
<td><input type="text" name="txtUsername" value="" /></td>
</tr>
<tr>
<td style="font-weight: bold">Password</td>
<td><input type="password" name="txtPassword" value="" /></td>
</tr>
<tr>
<td align="center" colspan="2" >
<input type="button" onclick="return reset();" value="Reset" name="btnReset" />
<input type="submit" onclick="return submit();" value="Submit" name="btnSubmit" />
</td>
</tr>
</tbody>
</table>
<script type="text/javascript" language="javascript">
function submit()
{
var username=frmlogin.document.txtUsername.vlaue;
var password=frmlogin.document.txtPassword.vlaue;
if(username=="")
{
alert("Username Must be filled");
return false;
}
if(password=="")
{
alert("Password Must be filled");
return false;
}
return true;
}
function reset()
{
frmlogin.document.txtUsername.vlaue="";
frmlogin.document.txtPassword.vlaue="";
return false;
}
</script>
</form>
</body>
</html>
I have html code above, and on successfully submit I call a servlet name Login.
But it directly call the servlet. It skips the java script function.
And if i remove the form then java script executes.
Your problem is that you gave the custom form validator function the same name as the form's default submit function form.submit(). The form's default submit function will have precedence in this context.
To fix this, you need to rename submit() to something else. E.g.
onclick="return validate();"
with
function validate() {
// ...
}
Unrelated to the concrete problem, you've several major bugs in your JS function, the following line for example is completely wrong:
var username=frmlogin.document.txtUsername.vlaue;
but that's subject for a different question if you can't figure it out.
Last but not least, I hope that you're aware that JS can be disabled/spoofed by the client? Ensure that you're also validating the form in the server side. See also our servlets wiki page for a concrete example.
add the onsubmit event handler for the form:
<form action="Login" method="post" name="frmLogin" onsubmit="return submit();">
devang you need to capture event for the submit button click if you want to execute that javascript code. i am having the same problem but i solved it using jquery click events, because the by default it submits directly without moving through the code . if any alternate solution is there , please letme know too
btw jquery code can be written as
$("input[name=btnSubmit]").click(function(e) {
..if fails conditions..
e.preventDefault();
});

JavaScript function is not defined

Consider:
File script.js,
function AdaugaComboBox(id, name){
var select_tag = document.getElementById(id);
select_tag.innerHTML += "<select name='"+name+"'><option value='0'>Selecteaza autor</option></select><br/>";
return true;
}
and file index.html:
<html>
<head>
<script src="js/script.js" type="text/javascript"></script>
</head>
<body>
<table>
<tr>
<td id="autori">...</td>
</tr>
<tr>
<td>
<input type="button"
value="Adauga autor"
onclick="AdaugaComboBox('autori', 'autori[]')"/>
</td>
</tr>
</table>
</body>
</html>
The scope of the function is to add a combo box to the specific TD in the TABLE. But when I press the button this error appears:
AdaugaComboBox is not defined
Why?
Update:
!!! I've fixed it. The problem was with another function.
If the script is included in your HTML, then it's possible that you don't have the path correct based on the location of the HTML file. Check with Firefox/Firebug to make sure that the JS file is being downloaded correctly.
Your HTML should be:
<html>
<head>
<script src="script.js" type="text/javascript"></script>
</head>
<body>
<table>
<tr>
<td id="autori">...</td>
</tr>
<tr>
<td>
<input type="button" value="Adauga autor" onclick="AdaugaComboBox('autori', 'autori[]')"/>
</td>
</tr>
</table>
</body>
</html>
You have to put a reference to the script.js file.
<script type="text/javascript" src="script.js"></script>

Runtime creation of tables in Javascript?

I my application "When a button is clicked it should create a new text field in the table". So i have done like this and its not working as well. So what could be the problem in the following snippet.
<html>
<script type="text/javascript" language="javascript">
var newtr=document.createElement("tr");
var newtd=document.createElement("td");
var output="<input type=\"textfield\"";
newtd.innerHtml=output;
newtr.appendChild(newtd);
function create_row()
{
document.getElementById("table1").appendChild(newtr);
}
</script>
<body>
<table id="table1">
<tr>
<td>
<input type-"textfield" name="tfield">
</td>
</tr>
<tr>
<td> <input type="button" name="button" value="new row" onclick="create_row();">
</tr>
</table>
</body>
</html>
I am using IE7.
In order to make this work in IE you should create a TBODY first then append the TRs in TBODY. If you don't do this IE cannot render the table content, HTML part will be created succesffully but it will not be seen on the page.
So you should modify your script to append TRs into the TBODY
Few remarks about your code:
You need to properly close tags: var output="<input type=\"textfield\"" is not valid
There's no input type="textfield" defined
The property to set the html of a given DOM element is called innerHTML and not innerHtml
You need to create a new tr element every time the button is clicked, so this should be inside the create_row function
You have a typo in your HTML: <input type-"textfield" name="tfield">. This should be changed to <input type="text" name="tfield" />
You are missing a head section in your document
I've tried cleaning your code a bit:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function create_row()
{
var newtr = document.createElement('tr');
var newtd = document.createElement('td');
var output = '<input type="text" name="test" />';
newtd.innerHTML = output;
newtr.appendChild(newtd);
document.getElementById('table1').appendChild(newtr);
}
</script>
</head>
<body>
<table id="table1">
<tr>
<td>
<input type="textfield" name="tfield" />
</td>
</tr>
<tr>
<td>
<input type="button" name="button" value="new row" onclick="create_row();" />
</td>
</tr>
</table>
</body>
</html>
UPDATE:
As pointed out by Guffa and Serkan answers in IE7 a tbody section needs to be used:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function create_row()
{
var newtr = document.createElement('tr');
var newtd = document.createElement('td');
var output = '<input type="text" name="test" />';
newtd.innerHTML = output;
newtr.appendChild(newtd);
document.getElementById('tablebody').appendChild(newtr);
}
</script>
</head>
<body>
<table id="table1">
<tbody id="tablebody">
<tr>
<td>
<input type="textfield" name="tfield" />
</td>
</tr>
<tr>
<td>
<input type="button" name="button" value="new row" onclick="create_row();" />
</td>
</tr>
</tbody>
</table>
</body>
</html>
You are missing a head tag.
The script should be in the head tag.
The html code for the input element is missing a closing bracket.
The type "textfield" does not exist.
The code only works once as you create the elements outside the function.
The method is innerHTML, not innerHtml.
You have to add the row to the tbody element, not the table itself.
I have tested this in Firefox 3 and IE 8 and it works:
<html>
<head>
<script type="text/javascript">
function create_row() {
var newtr=document.createElement("tr");
var newtd=document.createElement("td");
var output="<input type=\"text\">";
newtd.innerHTML=output;
newtr.appendChild(newtd);
document.getElementById("table1body").appendChild(newtr);
}
</script>
</head>
<body>
<table>
<tbody id="table1body">
<tr>
<td>
<input type-"textfield" name="tfield">
</td>
</tr>
<tr>
<td> <input type="button" name="button" value="new row" onclick="create_row();">
</tr>
</tbody>
</table>
</body>
</html>

Categories