GuidoG
GuidoG

Reputation: 12014

Calling javascript function from js file does not works

I have a simple javascript function on my page that works well, but I decided to move it away from index.html to a seperate js file.
The problem is that I cannot get it to execute from that seperate file.

This has been asked many many times here and the solution is always the same, but it just wont work for me. Also on google I can find lots of examples but they simply do not work for me. I tried many of them but with no luck.
So I must be missing something obvious I guess.

This is the header of my index.html

<?php session_start(); ?>

<html>
  <head>
    <title>Test</title>
    <link rel="stylesheet" href="test.css">
    <script src="gttJSFunctions.js" type="text/javascript"></script> 
  </head>

  <body>
    ...

The function in gttJSFunctions.js is called Logout()
As I said it works perfect when I put it inside my index.html, but I cant get it to execute from the seperate js file

this is the entire content of gttJSFunctions.js

<script language='JavaScript'>

    function Logout()
    {
        conf = confirm("Are you sure you want to logout?");
        if (conf)
        {
            window.open("logout.php", "_self");
        }
    }

Can someone please point out what I am doing wrong.

Upvotes: 1

Views: 243

Answers (2)

cusmar
cusmar

Reputation: 1913

Remove <script language='JavaScript'> from your gttJSFunctions.js file because it's already a Javascript file, you don't have to specify it.

Best practice: put <script ... /> tags at the bottom of your code, just before the </body> tag. It will improve your page speed and also, as Archer told, avoid you to deal with the DOM ready common mistake.

Upvotes: 1

Ivar
Ivar

Reputation: 6838

Your external JavaScript file should contain only JavaScript. No tags.

Just remove the <script language='JavaScript'> from your file and you are good to go.

Upvotes: 1

Related Questions