0

I am learning how to use JavaScript and I am having trouble correctly displaying the url within the document.write() function. I am writing the program with an actual url in the "href" portion but I can not write it in this question since it is apparently prohibited to do so. Is there something that I am missing?

<!doctype HTML>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title> Link Test </title>
</head>

<body>
    <a id="mylink" href="#"> Click me</a>
    <br>
    <script type="text/JavaScript">
        url = document.links.mylink.href document.write('The URL is ' + url)
    </script>
</body>

</html>

3 Answers 3

2

You are missing a semicolon if you want to have it on one line. Like this:

url = document.links.mylink.href; document.write('The URL is ' + url);

I would however recommend putting them on two lines, like this:

var url = document.links.mylink.href; 
document.write('The URL is ' + url);

..and using var to declare url as a variable, good practice

Sign up to request clarification or add additional context in comments.

5 Comments

As of ES2015, let or const are recommended over var.
Absolutely, just afraid if you start looking at ES2015 features directly when new to javascript, you might run into stuff that might not be supported in your browser, if you are new to the whole javascript scene. But you got my upvote! :)
If you're just learning JavaScript for the first time, I don't think you care very much about browsers 10 years old. Also, there are tools for that. In short: I don't care, and I think that neither should you.
We are not really talking about you and me right, but an answer to a person that is totally new to javascript. Isn't it good to try to avoid as many pitfalls you can run into as possible in the beginning? I personally would not use ES2015 directly ( check mobile: kangax.github.io/compat-table/es6 ) without using something like babel. And babel etc, imho, is to early to introduce to someone totally new to javascript.
That's fair. +1 regardless :)
1

Remove type="text/JavaScript" part from your code. Some modern browsers will give you an error on it.

Also I suggest you to write your statements in separate lines. JS engine understands your whole line as a one statement. So it gives an error. I suggest you to separate lines and put ; at the end of each line or you can add ; after the statement, if you want to write in one line. And also define your variables with var or let and const in ES6.

<!doctype HTML>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title> Link Test </title>
</head>

<body>
    <a id="mylink" href="#"> Click me</a>
    <br>
    <script>
        var url = document.links.mylink.href ;
        document.write('The URL is ' + url);
    </script>
</body>

</html>

2 Comments

He doesn't have to write them in separate lines, he can either add semicolons, separate the lines, or both. Both is the recommended course of action in most cases.
@MadaraUchiha has been changed
0
let url = document.links.mylink.href;
document.write('The URL is ' + link);

But still, I won't recommend using document.write method. You'd better add some html tag, where you can safely insert the result via innerHtml or innerText methods:

html:

<a id="mylink" href="#Test"> Click me</a>
<span id="result"></span>

js:

let url = document.links.mylink.href;
let result = document.getElementById('result');
result.innerText = url;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.