Microsoft Edge Refresh button does not execute the onLoad java script code - javascript

I have the following simple java script code which does not execute when I press the Refresh button in Microsoft Edge. None of the 3 alerts execute at refresh. It works well with Chrome.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script type="text/javascript">
jQuery(function() {
alert('jQuery');
});
function func() {
alert('func');
}
function load() {
alert('load');
}
window.onload = load();
func();
</script>
</head>
<body>
</body>
</html>
This code can be put in an HTML file (test.html).
Why does this happen? Thank you.

Maybe use window.addEventListener("load", load, false);. Sometimes, if one thing doesn't work, another will.

Related

Java script printing out a blank page

I have the following java script code
$("#btn_Print").click(function (e) {
e.preventDefault();
var printWindow = window.open("../private/analysisPrint.htm");
printWindow.print();
printWindow.close();
return false;
});
and the analysisPrint.htm page contains.
<!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></title>
</head>
<body>
<div>
<h4>
Print</h4>
First print page
</div>
</body>
</html>
The problem I am having is that I am always getting a blank page being printed. When i open the page directly and print its fine.
Any Ideas
You can (and probably should) wait for the page to load completely (including images):
var printWindow = window.open("../private/analysisPrint.htm");
$(printWindow).on('load', function() {
printWindow.print();
printWindow.close();
});

Use jQuery in a Safari Extension

For some reason in my global.html file jquery doesn't work for me.
here is the code that is in my global.html file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
<title>Hello World</title>
<meta name="generator" content="myHTMLEdit | www.myownapp.com" />
<script type="text/javascript">
function performCommand(event)
{
if (event.command === "alert") {
alert("event fired...");
var $x = $("meta[property=og:image]").attr("content");
alert($x);
alert("completed.");
}
}
function validateCommand(event)
{
if (event.command === "alert") {
event.target.disabled = !event.target.browserWindow.activeTab.url;
}
}
safari.application.addEventListener("command", performCommand, true);
safari.application.addEventListener("validate", validateCommand, true);
</script>
</head>
<body>
</body>
</html>
I'm using alerts to see if my jquery is working.
it will alert "event fired..." but stops there (no second alert).
however, if I delete the two lines of jquery both alerts pop up.
I'm creating a toolbar item that when I click it the event fires if that helps you solve this issue.
Only one error when I press inspect global page
Your selector sytax is invalid. If you check this JS and check your javascript console, you'll see the error message
Uncaught Error: Syntax error, unrecognized expression: meta[property=og:image]
I don't know how your meta tag looks like, but if it's like in the fiddle, the selector would be like
$('meta[og="image"]')
As you can see in the jQuery reference.
Note for future questions: This would be easier to resolve if you post the full error message in stead of an image containing only part of it.

"scrollHeight" not updated in "onscroll" event handler when using HtmlCtrl in an application but works correctly in IE9

When sample below code loads in IE9, it works correctly, but when loads in an html control in an application, scrollHeight not updated in onscroll event handler when I change the innerHTML content in the handler, and therefore the alert not to be displayed.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script>
function f()
{
var nBodyHeightOld = document.getElementById("div1").scrollHeight;
document.getElementById("div1").innerHTML += '<div>It is a test!</div>';
if (document.getElementById("div1").scrollHeight != nBodyHeightOld)
alert('scrollHeight changed!');
}
</script>
</head>
<body>
<div id='div1' style='overflow:scroll; height:300px' onscroll="f()">
<div style='height:400px'></div>
</div>
</body>
</html>
Becuse the application use IE8. To force application use IE9 add meta tag to html:
<meta http-equiv="X-UA-Compatible" content="IE=9" >

google chrome iframe body onLoad not working?

EDIT: It does work (sorry). Something in this script is causing it to stop in google chrome:
function checkLocation() {
var loc = top.location.href;
var suffix = "subpage.html";
if (loc.indexOf(suffix, loc.length - suffix.length) !== -1) {
top.location.href = "index.html";
}
}
Original post:
I have IE 9, FF 3.6.3, Chrome (18.0.1025.151) and Safari 5.1.5 all installed.
This works in all of the browsers except google chrome.
I have a HTML layout which contains a named iframe. The iframe src changes to display the different pages. On one of the pages I have a script which is loaded onLoad in the body tag. This script doesn't load when the page is loaded in the iframe in google chrome only - it works fine in other browsers. Also, if I load the page directly into google chrome (not via an iframe) it works just fine.
How do I fix this?
Here is an example code:
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html> <head> <title> Example Page </title> </head>
<body> View subpage<BR/>
<iframe name="targetFrame"> </iframe>
</body>
</html>
subpage.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html> <head> <title> Subpage </title>
<script type="text/javascript" src="subpage.js"> </script>
</head>
<body onLoad="initialise()"> Hello </body>
</html>
subpage.js
function initialise() {
alert("Script loaded.");
}
Thanks for looking.
Turns out its just a security exception
Unsafe JavaScript attempt to access frame
with URL file:///C:/Users/.../website/index.html
from frame
with URL file:///C:/Users/.../website/subfolder/subpage.html.
Domains, protocols and ports must match.
Once its online it should be fine.

Javascript onload run function help

Need help with my simple script that is supposed to auto click next after 5 seconds.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>
</title>
<script type="text/javascript">
window.onload = Next() {
setTimeout(Next() {
var next = document.getElementById("NEXT");
window.location.href = next.href;
}, 5000);
}
</script>
</head>
<body>
<div align="right">
<a id="NEXT" href="http://www.mysite.com/pictures.php?id=34">[ NEXT ]</a>
</div>
</body>
</html>
Your problem is that .click() only works on buttons.
Whilst were at it lets use unobtrusive javascript.
window.onload = function() {
setTimeout(function() {
var next = document.getElementById("NEXT")
window.location.href = next.href;
}, 5000);
}
Live example.
Edit
window.onload = Next() {
setTimeout(Next() {
Don't use the word Next() just use function()
To create functions you need either function () or function SomeName()
On a point of correctness, your script doesn't say it's javascript (you need to say which scripting language it is), and your html technically doesn't say it's HTML (it's missing a doctype declaration):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Title of the document</title>
<script type="text/javascript">
function next() {
// '1' dynamically generated when this page was generated by PHP,
// and will be '2' next time the page loads.
location = "pictures.php?id=1";
}
document.addEventListener("DOMContentLoaded", function(){setTimeout(next,5000);}, false);
</script>
</head>
<body>
...
</body>
</html>
This sounds like being pedantic, but IE in particular is really anal about having those things. Without a doctype declaration, it won't treat a document starting with as HTML code, but start to take fairly inaccurate guesses.

Categories

Resources