1

In my html document (which is in the xampp/htdocs directory), I'm using an external .js file. The .js file is in the same directory as my html file. I'm simply trying to use document.write() function and it's not printing anything.

I don't understand what I'm doing wrong. Whats the issue?

HTML file

<!DOCTYPE html>
<html>
    <head>
        <?php include 'include/head_elements.html'?>
        <script type="text/javascript" src="register.js"></script>
    </head>
    <body>
        <h1>Company Account creation</h1>
        <div id="registration_menu">
            <!--Elements are added and removed dynamically using JS-->
        </div>
        <script>
            hello();
            load_element_group("email_verification");
        </script>
    </body>
</html>

JS file

function hello(){
    document.write("Hello world")
}
18
  • you made a function, but you're not running it. Commented Apr 24, 2019 at 20:24
  • @StevenStark I added a script tag in the body section and called it Commented Apr 24, 2019 at 20:25
  • 1
    Did you see what I added to my previous comment? The most likely reason is that the script isn't loaded. Windows has an option to hide known extensions, so you should double check that your script is called register.js and not Register.js or register.js.js. Commented Apr 24, 2019 at 20:31
  • 1
    @tresf it ended up being a security issue of some kind.. i was using chrome. works fine now. thanks for all your help Commented Apr 24, 2019 at 20:46
  • 1
    It works fine on IE (if you open it via localhost/). What security issue exactly? How did you fix this? Commented Apr 24, 2019 at 20:48

1 Answer 1

1

Internet Explorer's security policy may block certain scripts from running on a local machine.

enter image description here

There are ways to avoid this -- such as by adding the XAMPP website as a trusted location -- but often this gets tricky since the default "Intranet Zone" is auto-configured on a PC and modifying that can have other consequences (different zones assume different settings, such as passing NTLM credentials to local websites).

See also https://stackoverflow.com/a/7038775/3196753

A quick fix often is to add the fully qualified domain name (FQDN) to the URL, but depending on the zone settings, this may still cause issues.

A final solution, and one many developers fall back on, is to actually use a registered DNS address, such as http://localtest.me/, which points back to localhost and should use the "Internet Zone".

As Chris G points out in the comments, this isn't typical. Normally localhost can be used without issue so I've provided an example Local Intranet setting which can cause this: enter image description here

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

6 Comments

Or just use localhost, which works for me in IE just fine (and Chrome). Your screenshot shows you have opened the file locally, in that case IE will indeed present a warning. This is about XAMPP though, which means there's a PHP based web server to avoid exactly those kind of problems already in place (and OP is using it, too, according to a previous comment).
Example security settings added to answer.
Sorry, but for me, even with all security set to "High", opening the page via localhost on IE11 works fine. No warning. "Hello World" is displayed. Plus, OP is using Chrome.
Interesting. Hello world disappears in my local when using "High". The Internet zone has its own auto-detection logic (influenced by three check boxes), and worse there are silent security changes that differ between domain and non-domain PCs. Regardless of these differences, Chrome should never (well, rarely -- it can prevent SSO, but never seen it break a JS include) be affected. I'm not at all offended if you downvote this answer, I assumed IE was the culprit.
You're right; I had localhost in "Trusted Sites". Still, OP was using Chrome, so the problem was something else. I can reproduce OP's issue if I manually block JavaScript for localhost in Chrome's settings, but OP said he got hello is not defined. My money is on a typo.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.