IE prompts (.js) Javascript file for download instead of executing it - javascript

Given a URL like: http:// host:port/app/whatever.js
IE(Internet Explorer) attempts to download whatever.js, instead of rendering(or executing) it on the browser. I am using IE8/WinXP
How can i make IE render(/execute) the .js instead (of prompting for a download)?
(On FireFox/Chrome whatever.js is rendered correctly, but not in IE.)

I realized that the behavior that IE is exhibiting is correct - thats the default behavior. IE will prompt for .js file to be downloaded if you give URL of .js directly in the browser bar.
But if the .js is invoked via HTML
<script src="whatever.js"> tag,
then the JavaScript is executed.
Therefore, there isn't really a case for .js to be rendered.

Related

Phantomjs: Modifying html dom before opening it as webpage

I need to process html files that have corrupted script files that are added to it via tag.
Im planning to remove all script tag present in the webpage via phantomjs.
But on opening the webpage via webpage.open(), phantomjs parse error is thrown since it cannot parse the JS content within the script tag.
Here is an example:
<html>
<head>
<script>
corrupted JS
if(dadadd
;
</script>
<body>
some content
</body>
</html>
Can someone help me on suggesting the right way to clean this webpage using phantomjs ?
It's not (easily) possible. You could download (not through opening the page, but rather making an Ajax request in page.evaluate()) the static html, then change according to your needs, then assign it to page.content.
This still might not work, because as soon as you assign it to page.content, you're saying that PhantomJS should interpret this source as a page from an unknown domain (about:blank). Since the page source contains all kinds of links/scripts/stylesheets without a domain name, you'll have to change those too in order for the page to successfully load all kinds of resources.
It might be easier to just have a proxy between PhantomJS and the internet with a custom rule to adjust the page source to your needs.

how to load javascript file from absolute network path

Is there a way i can define my script tag to use absolute path instead of relative path so that my JavaScript files are loaded from a network location?
This is what i have tried:
<script src="file:\\\MyDFSDirectory\Test\TestApp\Scripts\jquery-1.7.1.js"></script>
This does not work. in FF, i get the error Security Error: Content at http://localhost/Test/Test.html may not load or link to file:\\\MyDFSDirectory\Test\TestApp\Scripts\jquery-1.7.1.js
In IE, I dont see the file being downloaded. In Network Tab (IE Dev Toolbar), it shows Received 0 B. If i take the URL and paste it in the File Explorer, it opens the JS file.
What am i missing here?
You are indeed running up against the security model of the browsers. The only way around this is to run a web server locally and serve up the files that way.

View .bat as plain text from link using Internet Explorer

I need to allow users to click a link to a .bat file on an IIS 6.0 server directory and view that file as plain text. I have set the MIME type for .bat file to "text/plain." This works perfectly in both Chrome and Firefox. The way this works is that I have a button that launches a separate popup browser window to the .bat file. Example is below:
onclick=\"window.open(\'\/eemcontrolpanel\/jobs\/" + encodedFileName +"\',\'popUpWindow\',\'height=500,width=400,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes\');\">View Script</button>").scrollHeight;
As I said, this works in both Chrome and Firefox, but for some reason, IE 8 immediately closes the popup and asks me if I want to Run or Save the file.
I think IE goes by filename extension to detect its an executable file. Maybe you could give it another name by adding a header like:
'Content-Disposition: attachment; filename="thebat.txt"'

How do I stop a JavaScript file from downloading twice in JavaScript?

I have two user controls and both the user controls refer a same script path:
<script type="text/javascript" src="test.js"></script>
then what happens when the first user controls load then test.js will download in client and when second user control loads then test.js file will download again.
How do I check to see if the JavaScript file is already downloaded so it doesn't have to be downloaded again?
If I include the JavaScript file from the server side:
Page.ClientScript.RegisterClientScriptInclude("test",
Page.ClientScript.GetWebResourceUrl(this.GetType(),
"test.js"));
then how could I make sure it isn't included twice?
If the path is exactly the same, the client's browser should realize it already downloaded the file and not download it twice anyway. Can you confirm that the browser is indeed downloading it twice? If so, which browser(s)? Also check the browser cache settings.

Force a download of a text file in IE7 using javascript

Saw a similar question at Download a file using Javascript but this one is specific to IE and I'm using extjs and alfresco (opsoro). I can get a download prompt for text files fine in Opera, Chrome, and Firefox, but not IE.
Below is a list of what I've tried so far, which all work on other browsers except IE7.
document.location = downloadLocation;
window.open(downloadLocation,'Download');
location.href = downloadLocation;
When downloading other mimetypes (csv, xls), they download fine using any of the methods mentioned above.
You may need some help from the backend. Instead of serving the file statically, the backend app can load and serve the file contents with a content disposition header. That is guaranteed to invoke a download.
Content-disposition: attachment; filename=hello.txt
I don't know how you could do it with Alfresco, though.
Edit: Wait! Check this out: http://forums.alfresco.com/en/viewtopic.php?f=36&t=21356&p=70252

Categories