Forum Moderators: open

Message Too Old, No Replies

How to set long HTML elements inside javascript?

         

toplisek

1:37 pm on Mar 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How to set long HTML elements inside Javascript?

Example will work.
"messages":[
"HTML"
],

but long HTML will show an error.
Example will work.
"messages":[
"
<div id="header_inner1">
...
</div>

"
],

Fotiman

1:43 pm on Mar 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You've got a double quoted string within your double quoted string. So you need to either use single quotes for one of them, or escape the double quotes within the string:
"messages":[
"
<div id='header_inner1'>
...
</div>

"
],

or

"messages":[
"
<div id=\"header_inner1\">
...
</div>

"
],

toplisek

1:52 pm on Mar 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you.

toplisek

2:03 pm on Mar 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have tested. It will not work your suggestion. I'm not sure.
"messages":[
"<div class='myID1' id='mybanner' style='top: 0px;'>
<div id='header_inner1'>

</div>
</div>"
],

Fotiman

2:28 pm on Mar 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is your JavaScript in an external .js file (it should be) or is it inline in your HTML like this:
<script>
"messages":[
....
</script>

If it's inline, then you also need to escape the slashes, or the browser may think that </div> is closing an element. You don't need to worry about that if the file is external and included like this:
<script src="..."></script>

toplisek

3:57 pm on Mar 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have done external file as testing.
"messages":[
{/literal}{include file="myfile.tpl"}{literal}
],

It seems still an error.

toplisek

4:01 pm on Mar 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have an error:
Error: SyntaxError: expected expression, got '<'
<div class="home" id="mybanner" style="top: 0px;">

Fotiman

5:42 pm on Mar 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




I have done external file as testing.
"messages":[
{/literal}{include file="myfile.tpl"}{literal}
],

It seems still an error.

Now you're missing quotes around the entire value. Should be something like this:

"messages":[
'{/literal}{include file="myfile.tpl"}{literal}'
],

Fotiman

5:46 pm on Mar 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry, I just realized that you're trying to do multi-line strings. You'd need to do string concatenation. Something like this:

"messages":[
"<div class='myID1' id='mybanner' style='top: 0px;'>" +
"<div id='header_inner1'>" +
"" +
"</div>" +
"</div>"
],

toplisek

2:58 pm on Mar 21, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you very much for all replies. It is working perfect.