Why Chrome fails to run jQuery - javascript

I have latest version of google chrome.The following script runs in all browsers except in google chrome. Can anyone give me any suggestion on what to do to run it in chrome.
<!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">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt");
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>
</body>
</html>

Chrome by default does not allow ajax requests to access the filesystem. You can get around it by setting an argument in the shortcut that you use to open chrome, however I'd suggest instead just not testing from the filesystem to begin with. It's not too difficult to setup a very basic webserver.
For reference, file://.../myfile.html would be considered working from the filesystem, while http://localhost/myfile.html would be working from a local webserver.

Related

External scripts not working in Windows Phone App 8.1

I've added a WebView into my project. If I navigate to the html file that contains external javascript files they don't work. Bit if I write the javascript code hardcoded into the html file it works properly.
Here is the html portion:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="testjs.js"></script>
</head>
<body>
<div id="abcd"></div>
<br/>
hello! this is a simple html block.
</body>
</html>
Here is the javascript portion:
document.getElementById("abcd").innerHTML = "New text!";
When I open the webview it shows
hello! this is a simple html block.,
but if the javascript code worked it would show
New Test!
hello! this is a simple html block.
Here is the folder structure:
src="testjs.js" is correct. The issue is not related to the path.
[Update 1]
Your code should work as we usually do for web development. But seems we need to make sure the html element used in the script has been initialized, so we have to put the to the end of the body element. Please try the following html code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div id="abcd">a</div>
<br />
hello! this is a simple html block.
<script type="text/javascript" src="testjs.js"></script>
</body>
</html>
[Update 2]
Also check if you have changed the build action for testjs.js to content. go to Solution Explorer -> right click on testjs.js -> Properties -> change Build Action to Content. That will make sure the .js file will be deployed as part of you app.
WebView msdn page suggests that for security reasons you can not link to local content except for:
However, you can still link to HTML content in the app package using the ms-appx-web scheme, and to web content using the http and https URI schemes.
So you should be able to link to your script file using ms-appx-web:///testjs.js url.

why jscript does not work in Chrome browser on iOS 7?

Please explain me why my simple script does not work in Chrome browser on iOS 7.
It works in safari, android devices, desktop browser BUT not in ios chrome...
Browser says nothing... not an error, no the message.
Why?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
alert("11");
</script>
</head>
<body>
</body>
</html>
Try to wrap 11 in quotes:
alert("11");

Disable page redirects using Greasemonkey

A website I wish to tweak is using window.location in order to redirect specific users to a blocking page. That website is doing it in plain <script> tag, so it is impossible to bypass it by overriding the onload event using document.body.setAttribute('onload','');.
Is there another way to inject my code to the page without using Firefox extensions such as NoScript?
<!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>
<script type="text/javascript">
if (1) window.location="http://example.net"
</script>
</head>
<body></body>
</html>
This question is tagged "Greasemonkey", but GM cannot/will-not run your script before the redirect script fires. You'd have to write a firefox add-on to do that. Might poke around https://addons.mozilla.org/en-US/firefox/ first.
Sometimes you can use adblock to stop the load of the offending script.
NoScript might be the most cost-effective way, if the site's usable without javascript (although GM javascript will still run -- so you could replace lost functionality with GM code, maybe).
This question appears to be related to this one.

window.onload() is not firing with IE 8 in first shot

I am trying to make my pages work correctly with IE 8, I found out from here: http://www.masykur.web.id/post/How-to-Make-Our-Website-to-be-Ready-for-IE8.aspx
that, my page has to be XHTML 1.0 compliant and atleast CSS 2.1 compliant, I made my page and CSS compliant with only few warnings, but still window.onload() is not firing. Does anybody encountered this problem?
here is the code snippet:
<!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" lang="en" xml:lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8"/>
<title>Testing</title>
<link rel="stylesheet" href="test.css" type="text/css"></link>
<script type="text/javascript" src="login.js"></script>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript">
window.onload = function()
{
// Not coming here at all on first shot
}
</script>
</head>
<body>
.
.
.
However refreshing the page seems to make it work.
Am I missing something here?
UPDATE:
One of the IE addons created this problem, after disabling its working fine. Thanks for your time and answers :)
For IE try:
window.onload = new function() { alert('hello');};
This is a pretty old thread but I found a solution that might help others.
I was adding a function to window.onload via a dynamically injected script (really to mimic an external script load for reasons which are not important in this context). As stated in this post, on the first load within IE8, the window.onload would not fire, but all subsequent calls would.
I found out that if I put this in the HTML file as an internal script, it would work everytime:
var windowOnload = window.onload || function() {};
window.onload = function() { windowOnload(); };
All the above code does is "initializes" IE8's window.onload unobtrusively. I suspect that IE8 fails to trigger window.onload the first time if it is called from an external script as the onload event isn't attached yet to window (or in tech terms, its typeof is undefined). It seems that the first time that's what IE8 is doing: attaching onload to window without executing it properly.
The above code then becomes quite obvious: We are merely forcing IE8 to recognize the onload event. We don't care what gets executed, or what doesn't, so we simply make sure to pipe on through any existing window.onload code that is already defined (just as a precaution).
It is important to have this as an internal script to the HTML (at least from my testing).
Your HTML would thus look something like this (the relevant parts):
<script type="text/javascript">
var windowOnload=window.onload||function(){};window.onload=function(){windowOnload();};
</script>
<script type="text/javascript" src="script.js">
</script>
From my testing, after clearing cache, and reloading the page, I have gotten window.onload to successfully trigger each time.
I hope this helps.
You could have an error in your JavaScript's, if that happens, any JavaScript after that will not function correctly.
Try to remove the reference to login.js and common.js and try an alert within your problematic function.
I don't have IE8 to personally test, but what does this test do?
<!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" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test IE 8</title>
<script type="text/javascript">
/* <![CDATA[ */
window.onload = function(){alert('Good morning!');}
/* ]]> */
</script>
</head>
<body>
<h1>Hello</h1>
<body>
</html>
If this test works as expected, try the CDATA bit around your internal JavaScript block.
And then, if that does not work as expected, there is probably something in the external JavaScript above it that prevents your onload from firing. The previous poster mentioned this. At that point, try your error console or debugger to point the way.
onload fires after ALL your content has loaded (including external images etc). It's possible those resources are taking a long time to load on the first go (before they are cached). Another possibility is an error in your code that only affects IE as is stopping your scripts (but only the first time is odd).
If you are getting different results using Apache vs. another web server (IIS?) and comparing the end result using IE8, then the differrence must be in the content type header being sent. Get the wget utility for your platform and see the headers that are produced. If you are on Windows, then the Portable Apps version of the GUI wget is pretty nice.
The following code works for me. When I load the page in Firefox I see the alert instantly. When I first load the page in IE 8 it warns me about active content. If I allow the blocked content it asks me to confirm, which I do. Then the alert appears as expected. If this does not work for you, try IE 8 on a different computer or start eliminating code in your page to check for errors. You could do a binary search: comment out the first half of the page and see if the alert appears; if it still does not, then uncomment out the first half and comment out the second half. Repeat as needed until you've narrowed it down to the offending code. Incidentally you don't need XHTML for IE8 compliance. HTML works fine and actually has some advantages.
<!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" lang="en" xml:lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8"/>
<title></title>
<style type="text/css">
</style>
<script type="text/javascript">
window.onload=function() { alert('hello');};
</script>
</head>
<body>
</body>
</html>
I know this is kinda old question, but I just faced the same thing.
window.onload=function() { alert('hello');};
is treated differently than
window.onload= new function() { alert('hello');};
The keyword here is new.
My JS was terminating whenever it reaches (onload=) part until I added the word'new' before 'function'. Even though it worked fine without 'new' in my localhost, but once I put it online, it doesn't work until I add 'new'.
Old question but I had the same issue but it turned out to be another problem. My problem was that I did <script type="application/javascript"> which <IE9 does not understand or try to run even. For it to work for older browsers you still have to use text/javascript even though this isn't technically the correct type...

Download Javascript generated XML in IE6

I'd like to use Javascript to make IE6 download a file. It'll be created on the fly using Javascript. This file doesn't exist on a webserver. Here's a small example:
<!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" xml:lang="en" lang="en">
<head>
<script type="text/javascript">
function clicked() {
var xml = "<data>Just for testing</data>";
document.open("text/xml", "replace");
document.write(xml);
}
</script>
</head>
<body>
<input type="button" value="Download" onclick="clicked();" />
</body>
</html>
Instead of loading the xml in the browser window, I want it to cause IE6 to prompt the user where to download the data to so that it can be saved without them having to use File -> Save as. Any ideas?
For IE6 you should be able to use document.execCommand() after your document.write():
document.execCommand('SaveAs',true,'file.xml');
This is not part of any standard and will only work in IE flavor browsers.
If your data must be generated client side, then you can post it back to the server so that it can be returned as a downloadable file.
No, this is not possible. A web-browser strictly doesn't allow this, as the ability to save files to disk through JavaScript only, would be very dangerous, even if the confirmation popup shows up.
EDIT: Thanks to other answers, I found out (not surprisingly) that this behavior is possible with some versione of IE.

Categories

Resources