0

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")
}
Amessihel
  • 5,312
  • 1
  • 13
  • 37
Andrew Kor
  • 557
  • 6
  • 17

1 Answers1

0

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

tresf
  • 5,357
  • 4
  • 34
  • 92
  • 1
    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). – ChrisG Apr 24 '19 at 20:57
  • Example security settings added to answer. – tresf Apr 24 '19 at 21:07
  • 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. – ChrisG Apr 24 '19 at 21:23
  • 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. – tresf Apr 24 '19 at 21:29
  • 1
    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. – ChrisG Apr 24 '19 at 21:35
  • If "security" was deemed the culprit and it wasn't IE related, my next guess would be local file permissions. – tresf Apr 24 '19 at 22:02