Click here to Skip to main content
14,386,126 members

Why can I put this code snippet only in the same page and not in an external js file?

Joan M asked:

Open original thread

I'm finishing the development of my time tracker php application.

After working a little with it (It's wonderful having something that suits perfectly your needs) i've noticed it would be a good thing having the possibility to set the number of lines to be shown in each page.

I've put a small editbox that in javascript it sets a cookie when enter key is pressed and then reloads the page to show the changes.

It works perfectly if I put the javascript code in the head of the php document.

But it doesn't work when I put the code inside a js file that is linked in the same head section of the same php document.

The function is this one:

function changeLiniesPerPagina(e)
  if(e.keyCode == 13)
    document.cookie = "cookieName="+document.getElementById("lines").value+"; expires=Fri, 31 Dec 2049 12:00:00 UTC; path=/;";

If I use the "Inspect" option in the browser, I can see the message it appears when the function is located in an external js file is:

VM715 phpfilename.php:27 Uncaught ReferenceError: changeLiniesPerPagina is not defined
    at HTMLInputElement.onkeypress (VM715 phpfilename.php:27)
onkeypress @ VM715 phpfilename.php:27

Any idea?

Thank you very much!

What I have tried:

Checked the syntax, put the code snippet in different js files...
Tags: Javascript, PHP


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100