Javascript not working in apache-served html - javascript

I set up an apache server on gentoo, I want to be able to run javascript code in my html files served by apache.
If I manually open up the html file that lives on the apache server in Chrome, the javascript works fine. If I browse to the html file using the server address via apache, it does not execute. This is my test html, it's very simple:
<html>
<h2>js test</h2>
<script>
alert("Hey");
</script>
</html>
I haven't ever seen this behavior before. Any ideas?
EDIT:
What I mean by opening manually and using the server address is this:
file:///mnt/server/path/to/file.html (opening manually)
http://server/path/to/file.html (browsing to apache server)
EDIT 2:
No errors in apache's error_log. At first I was using jquery to try to do some ajax stuff, but I tried to simplify everything to the max for this question.

As pointed out by #Blender, the HTML is missing the typical head.../head and body.../body sections. Maybe this matters in the HTML loading properly and the browser triggering the script portions.
Also, pop ups like alert() can be blocked by pop up blockers in the browser that can differentiate a site vs a file url.

Related

Beginner Question - How do I get my JS code to show on a browser?

I'm using VS code as my code editor (not sure if it's relevant). My first project is on Changing Background Color. Once I'm done writing my codes, how do I get them to show on a browser?
I'm not sure how to get it to show on a browser.
You want see your code in browser you will open live server of the code docoument -
open a HTML file and right-click on the editor and click on Open with Live Server .
or just type this url in browser
http://127.0.0.1:5500/
5500 is a default port
or just install an extensions live server it will directly open the browser.
The best way in my opnion to execute JavaScript in your browser is to put it inside a script element inside an HTML document.
I mean that in the same folder that you have the js file, create an html file and in there put the script link of the file.
<script src="script.js" defer></script>
This need to be in the <head></head>
hi in vscode download live server then click html file with right click and open with browser

Can't load javascript file in localhost using xampp

I am new at web development and I want to upload a main.js scrip in my PHP file but it gives me this error
I used a script tag to upload the js file like this:-
<script type="text/javascript" src="../assets/js/main.js"></script>
This could be due to many things:
Check ad/script blocker
Check if the browser can load it from the view page source option
Try pasting it before all scripts call it could be due to some functions in previous scripts, I faced this issue before.
This might help.

Why can't I get my script tag src's to work?

I have been coding up a localhost, and I made the localhost by using of course a JavaScript file to do so, and I then made it reference an HTML file. However, I noticed that when I am using localhost to serve up the HTML file I get this error:
"GET http://localhost:3333/filetesting.js"
The filetesting.js is that js file, there are also other things I'm referencing too, like websites. I'm referencing it by using script tag src.
I looked at the network on developer tools of it and it says it's a 404 error not found. I'm trying to figure out how to reference my script tag src's without having localhost:3333 go before it.
When I run the HTML file without using the localhost, it works just fine when it comes to the script tag src's. If you do not entirely understand what I'm asking for, just ask.
Assuming that your script will always reside in the root level of your website, you can simply target it with the root-relative prefix /:
<script src="/filetesting.js"></script>
This will load your script from the root, regardless of the site the file is hosted on. For example, on http://localhost:3333/ it will load the file from http://localhost:3333/filetesting.js, and from http://localhost:3333/folder/, it will attempt to load the file from the same location.
If you move your files over to a proper website, it will still work the same way: www.example.com will look for the file at www.example.com/filetesting.js, and www.example.com/folder/ will look for the same file at www.example.com/filetesting.js.
Hope this helps! :)

Loading an XML locally into JavaScript without having a web server available?

I've made an script for processing XML:
Extracts parts of the XML-data and displays them on a HTML-/CSS-page.
Currently I use the stringified content of some arbitrary file for doing my development. Written as a string-literal into my JavaScript.
Works great. But now comes the problem:
Of course I would like to load whatever (equal-structured) XML-files. Instead of having it in my code as a string-literal.
Normally I would load the files into my script via Ajax.
But I can't install a web-server on these computer.
I'm within an enterprise and it isn't possible to install any additional software. Restricted via group-policies etc. No chance. Forget it!
As far as I know it isn't possible to use Ajax without a web-server because Ajax communicates via the http-protocol.
So here's my questions:
I there (perhaps) a possibility to use Ajax without a web-server?
And in case of impossible:
Have I got any Ajax-alternatives to load XML-data into my script?
You can embed your xml inside script tag in your html like this:
console.log(document.getElementById('file').innerHTML)
<script type="text/xml" id="file">
<root><foo><bar></bar></foo></root>
</script>

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.

Categories