1

I have a question about Gedit. I am trying to learn JavaScript. I went to http://www.w3schools.com/js/ and tried the Date and Time example.

Date and Time example code:
Source: http://www.w3schools.com/js/tryit.asp?filename=tryjs_myfirst

<!DOCTYPE html>
<html>
<body>

<h1>My First JavaScript</h1>

<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>

<p id="demo"></p>

</body>
</html> 

After a while I copied and pasted the script into Gedit and found that it didn't work the way that it did on the W3Schools website. I am able to see something in the browser, but it does nothing when I click the button. Here is a screenshot of what I see:

JavaScript

The pop-up blocker is off, and I don't know what else to do. Are there some plugins that I could use to change this? I don't need anything fancy, I really like Gedit for the most part, I would just like to be able to use it for all my coding needs. All I have learned so far is HTML and CSS.

7
  • You mean it doesn't color/highlight in the same way?
    – user423626
    Jul 26, 2016 at 11:53
  • No the button is there but it has no function. On the website it will provide a date and time when clicked. But it does nothing when I copy it into gedit. Jul 26, 2016 at 11:57
  • 1
    Tip: if you're going to learn javascript, you might enjoy the website JSFiddle. It's an online editor for JS, HTML and CSS which allows you to experiment with these languages from a single interface. Your code example would look like this in JSFiddle: jsfiddle.net/vnaaprmd Jul 26, 2016 at 15:34
  • 2
    As a public service, I would like to point out that W3Schools has a reputation for not always being of the highest quality. It's a lot better than it used to be, but the reference material at developer.mozilla.org/en-US may be more reliable and up to date.
    – IMSoP
    Jul 26, 2016 at 17:12
  • From your question and comments, this appears to be a web development question (and a massive amount of confusion as to how web pages work), which is not very well suited here. gedit has nothing to do with your problem. Jul 26, 2016 at 19:18

2 Answers 2

14

From the comments:

No the button is there but it has no function. On the website it will provide a date and time when clicked. But it does nothing when I copy it into gedit.

Gedit is a text editor. It can edit text (and source code, etc.). To run the code, save it with a .html (for example, as datetime.html) because this is HTML code.

Then double-click the resulting HTML file to open (and thus run) it in your browser of choice (Firefox, Chromium, Chrome etc.).

As to why:

On the website it will provide a date and time when clicked.

Assuming you mean this, https://i.stack.imgur.com/jLg33.png

it is because the thing in the screenshot is not an editor. It is the result.

If you want an editor that also provides the result as a preview, try Atom (there may be others). Write HTML in it and press Ctrl+Shift+M.

4
  • Perhaps I am asking the wrong question. Your comment is correct, you have to save it to a .html file and double click it, but this has been done already with no success. Jul 26, 2016 at 12:44
  • @BJsgoodlife Can you provide screenshots?
    – user423626
    Jul 26, 2016 at 12:49
  • @BJsgoodlife Also see the note in the_velour_fog's answer
    – user423626
    Jul 26, 2016 at 12:51
  • @BJsgoodlife Look at the JS console output.
    – user423626
    Jul 26, 2016 at 13:07
5

you need to

  • copy the script into gedit then
  • save the code as a file , then
  • load the file in the browser,

Ctrl + o is the shortcut to open a file directly from a browser.
Note: for security reasons browsers sometimes won't allow direct execution of javascript from local files. Because the browser expects it should have been served by a webserver.

1
  • 1
    I think that last "Note:" is probably the actual answer. I've been stung by this myself, trying to play around with JS Canvas stuff, and had a total facepalm moment when I realised.
    – IMSoP
    Jul 26, 2016 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .