-1

Here is my document.write function which is not working. Please tell me and help in resolving the issues. I want to place this "document.write" in one single .js file and include that in all my html pages. But at first "document.write" is not working. :( thanks

document.write("
<table><td><img src=allpages.png style=width: 80%;></img><br>
<select name='Archive' value='Archive'style=' background-color: black;
padding: 3px; margin: 0px; margin-top: 5px; border-radius: 10px;
box-shadow: 0px 3px 0px ‪#‎CCC‬, 0px -1px ‪#‎FFF‬ inset;
color: yellow; border: medium none;
outline: medium none; text-align: right;
display: inline-block; width: 80%;
cursor: pointer; '>
<option onclick=javascript:location.href=index.html><a href='#'>صفحہ اول</a></option>
<option onclick=javascript:location.href=2.html><a href='2.html'>دوسرا صفحہ</a></option>
<option onclick=javascript:location.href=3.html>تیسرا صفحہ</option>
<option onclick=javascript:location.href=4.html>چوتھا صفحہ</option>
</select></td>

<td><img src=allnewspapers.png style= 'width: 80%;'></img><br>
<select name=Archive value=Archive
style='background-color: black;
padding: 3px; margin: 0px; margin-top: 5px;
-moz-border-radius: 4px;
border-radius: 10px;
box-shadow: 0px 3px 0px #CCC, 0px -1px #FFF inset;
color: yellow; border: medium none;
outline: medium none; text-align: right;
display: inline-block; width: 80%;
cursor: pointer;'>
<option>جنوری 2015</option>
<option>فروری 2015</option>
</select></td></table>");
Zeshan Sajid
  • 559
  • 3
  • 14
  • 1
    you will need to put the html right first, you're not qouting where you should, for instance , line 1 `img src=allpages.png` needs to be `img src='allpages.png'` – Billy Jan 03 '16 at 11:26
  • @Billy - no it doesn't. `src=allpages.png` is fine - you don't need quotes there. But `style=width: 80%;` needs to be `style='width: 80%;'` because otherwise the space will terminate the attribute value too early. – Alohci Jan 03 '16 at 12:43
  • @Alohci, you're correct. I did actually know that without spaces or certain chars was ok, I just grabbed the first attr I saw, my bad. – Billy Jan 03 '16 at 12:54
  • i did the same. yet it says "Error: Expected: ;" – Zeshan Sajid Jan 03 '16 at 13:10

3 Answers3

0

I wonder if this is the best way of doing that. However, your code works when used without the line breaks at the end of each line.

document.write("<table><td><img src=allpages.png style=width: 80%;></img><br><select name='Archive' value='Archive'style=' background-color: black;padding: 3px; margin: 0px; margin-top: 5px; border-radius: 10px;box-shadow: 0px 3px 0px ‪#‎CCC‬, 0px -1px ‪#‎FFF‬ inset;color: yellow; border: medium none;outline: medium none; text-align: right;display: inline-block; width: 80%;cursor: pointer; '><option onclick=javascript:location.href=index.html><a href='#'>صفحہ اول</a></option><option onclick=javascript:location.href=2.html><a href='2.html'>دوسرا صفحہ</a></option><option onclick=javascript:location.href=3.html>تیسرا صفحہ</option><option onclick=javascript:location.href=4.html>چوتھا صفحہ</option></select></td><td><img src=allnewspapers.png style= 'width: 80%;'></img><br><select name=Archive value=Archivestyle='background-color: black;padding: 3px; margin: 0px; margin-top: 5px;-moz-border-radius: 4px;border-radius: 10px;box-shadow: 0px 3px 0px #CCC, 0px -1px #FFF inset;color: yellow; border: medium none;outline: medium none; text-align: right;display: inline-block; width: 80%;cursor: pointer;'><option>جنوری 2015</option><option>فروری 2015</option></select></td></table>");
Ishan Madhusanka
  • 641
  • 1
  • 11
  • 21
0

Avoid pressing enter while typing inside write function. It works!!!

document.write("<table><td><img src='allpages.png' style='width: 80%;'></img><br>    <select name='Archive' value='Archive'style=' background-color: black; padding: 3px; margin: 0px; margin-top: 5px; border-radius: 10px;    box-shadow: 0px 3px 0px #ccc,0px -1px ‪#‎FFF‬ inset;    color: yellow; border: medium none;    outline: medium none; text-align: right;    display: inline-block; width: 80%;    cursor: pointer;'>    <option onclick=javascript:location.href=index.html><a href='#'>صفحہ اول</a></option>  <option onclick=javascript:location.href=2.html><a href='2.html'>دوسرا صفحہ</a></option>    <option onclick=javascript:location.href=3.html>تیسرا صفحہ</option>    <option onclick=javascript:location.href=4.html>چوتھا صفحہ</option></select></td> <td><img src='allnewspapers.png' style= 'width: 80%;'></img><br>    <select name=Archive value=Archive    style='background-color: black;   padding: 3px; margin: 0px; margin-top: 5px;    -moz-border-radius: 4px;    border-radius: 10px;    box-shadow: 0px 3px 0px #CCC, 0px -1px #FFF inset;    color: yellow; border: medium none;    outline: medium none; text-align: right;    display: inline-block; width: 80%;    cursor: pointer;'>   <option>جنوری 2015</option>   <option>فروری 2015</option>  </select></td></table>");
Ashok Lama
  • 48
  • 5
0

The problem itself is caused by the fact that strings are only allowed to be on single lines. So this is invalid:

var syntaxErrorString = "test
test";

There are three ways around this:

  1. Concatenation:

    var concatenatedString = "test\n" +
    "test";
    
  2. Escaping:

    var escapedString = "test\n\
    test";
    
  3. Condensing:

    var condensedString = "test\ntest";
    

I'd suggest going with "none of the above". It is far easier to have some server-side language such as PHP, define your block of HTML as, say, navigation.html, and then just write <?php include "navigation.html"; ?> when you want it. Much better.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592