Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript function, pass HTML as parameter

I have two functions in my script, one that outputs some long HTML string, the other one that takes this string as a parameter and processes it.

function myFirstFunction() {
    //output some HTML
    return myHTML;
}

var myHTML = myFirstFunction();

function mySecondFunction(myHTML) {
    //do something with parameter
}

For some reason that I can't figure out, the Chrome JS Console keeps giving me the following error: "Uncaught SyntaxError: Unexpected token <"

I thought maybe that was due to the fact, that the outputed HTML was pretty long since it seems to be working with small chunks of HTML. Any thoughts? Thx!

like image 895
sf89 Avatar asked Apr 28 '26 15:04

sf89


2 Answers

Here's problem:

myHTML is a HTML string like this:

var myHTML ="<div id="foo"><div id='hello'>hello</div><div id="bar">bar'asdf'asf"sdf"&soidf;</div></div>";

That won't work because you have quotes and stuff inside it that are NOT escaped.

If you used innerHTML to get the HTML of an element on the page, this wouldn't be a problem.

like image 129
sachleen Avatar answered May 01 '26 05:05

sachleen


myHTML is constructed with some < or > extra so verify the html string

like image 30
Santiago Elvira Ramirez Avatar answered May 01 '26 05:05

Santiago Elvira Ramirez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!