1

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.

11
  • 1
    Well, you have no doctype, no head tag and no body tag. Do you get any errors in your console? Commented Mar 17, 2013 at 5:30
  • 1
    Your question is hard to decipher. What do you mean by "manually open up the html file that lives on the apache server in Chrome", vs "browse to the html file using the server address via apache"? These are not explicit enough descriptions to understand exactly what you're doing. Commented Mar 17, 2013 at 5:32
  • 1
    That's not how JS in HTML works, HTML JS executes in your browser, if you just navigate to the HTML file server address nothing will happen. The running or not-running of JavaScript has NOTHING to do with apache Commented Mar 17, 2013 at 5:32
  • Edited post to describe how I was accessing the html files Commented Mar 17, 2013 at 5:34
  • Is your Apache serving the right content type on those files? (Check in Chrome's Network tab) Commented Mar 17, 2013 at 5:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.