0

I am a Javascript beginner.

I try to have these two pieces of html placed into the markup. The anchors' IDs are set from tab-1 to tab-6.

This is what I've wrote so far. It doesn't do anything. Anybody got some ideas?

<script>

    if (window.location.hash == "#tab-1" || "#tab-2" || "#tab-3" || "#tab-4" || "#tab-5" || "#tab-6") {
    } else { 
      document.write("<div id="tagline"><p id="quote">"Play for fun or don't play at all!"</p> <p id="namer">- S. Pussehl</p> </div>") 
    }

OddDev
  • 3,644
  • 5
  • 30
  • 53
  • 2
    Note: In your `if`, you need to place `window.location.hash ==` everywhere, because `"#tab-2"` on its own is always true. – Siguza Apr 27 '15 at 11:18
  • 1
    And you need to escape the `"` character inside a double-quoted string. – Siguza Apr 27 '15 at 11:19

2 Answers2

1

There are some errors in your code:

  1. If expression. You need to separate all expressions and to check what you want.
  2. You can't separate the line without symbol "+". Or everything in the same line.
  3. Quotes inside quotes. Or use '""', "''", or use \' = quote

Try to use it:

if (window.location.hash == "#tab-1" || window.location.hash == "#tab-2"
 || window.location.hash == "#tab-3" || window.location.hash == "#tab-4" 
 || window.location.hash == "#tab-5" || window.location.hash == "#tab-6"
) {
 // Some code 
} else { document.write('<div id="tagline">' +
                            '<p id="quote">"Play for fun or dont play at all!"</p>' +
                            '<p id="namer">- S. Pussehl</p> </div>'
           ); 
    } 
Anatoli
  • 712
  • 1
  • 10
  • 21
0

you are wrapping your HTML with double quote and giving id to your elements with double quote so it won't work.

You should learn string concatenation in JavaScript.

And your JavaScript should like below.

document.write('<div id="tagline"> <p id="quote">"Play for fun or don\'t play at all!"</p> <p id="namer">- S. Pussehl</p> </div>');

UPDATE : And in your if condition you should compare window.location.hash with all, individually.

shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50