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")
}
Amessihel
  • 5,891
  • 3
  • 16
  • 40
Andrew Kor
  • 579
  • 7
  • 19
  • you made a function, but you're not running it. – Steven Stark Apr 24 '19 at 20:24
  • @StevenStark I added a script tag in the body section and called it – Andrew Kor Apr 24 '19 at 20:25
  • Works fine for me. I see the `

    `, and below it says "Hello World". 1. check your browser console for error messages 2. press Ctrl+U to look at the source HTML and click the underlined script src _register.js_. Do you see the script?

    –  Apr 24 '19 at 20:25
  • It only shows the `

    ` for me

    – Andrew Kor Apr 24 '19 at 20:26
  • Are you getting any error, or just no output? – Anis R. Apr 24 '19 at 20:27
  • @Amessihel Still doesnt work. Tried opening the document not using `localhost` (just opening like a regular file) and same thing – Andrew Kor Apr 24 '19 at 20:27
  • @AnisR. No output – Andrew Kor Apr 24 '19 at 20:27
  • 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`. –  Apr 24 '19 at 20:31
  • @ChrisG it is `register.js`. @Amessihel : The browser console says `hello is not defined` – Andrew Kor Apr 24 '19 at 20:33
  • @Amessihel Opening it locally works fine (after renaming `index.php` to `index.html`). –  Apr 24 '19 at 20:35
  • Did you do what I suggested and test the `src` in the HTML source view? –  Apr 24 '19 at 20:35
  • Just a thought, but some security policies prevent running scripts in IE. @AndrewKor you using IE? https://i.imgur.com/DXwAaC2.png – tresf Apr 24 '19 at 20:41
  • @AndrewKor - apologies, I didn't notice that tbh. I have added an answer here to make up for my mistake ;) – Steven Stark Apr 24 '19 at 20:46
  • 1
    @tresf it ended up being a security issue of some kind.. i was using chrome. works fine now. thanks for all your help – Andrew Kor Apr 24 '19 at 20:46
  • @AndrewKor Ok, I'll provide it in answer form. – tresf Apr 24 '19 at 20:47
  • 1
    It works fine on IE (if you open it via localhost/). What security issue exactly? How did you fix this? –  Apr 24 '19 at 20:48
  • Added screenshot to answer. Simply setting `Local Intranet` to High will cause this. – tresf Apr 24 '19 at 21:10
  • @AndrewKor Can you help demystify some questions on the accepted answer? – tresf Apr 24 '19 at 21:36

1 Answers1

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

tresf
  • 7,103
  • 6
  • 40
  • 101
  • 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). –  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. –  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. –  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