Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, July 28, 2016

Jquery append using multiline

Jquery append using multiline


I have been working on a project that dynamically creates a javascript file using ASP.NET which is called from another site.

This jquery javascript file appends a div and fills it with a rather large HTML segment and in order to do that I need to turn the segment into a string like so:

$(document).ready(function(){     var html = "Giving this magazine such a lofty epithet may seem a bit presumptuous, but for a non scientifically trained outsider this magazine offers a fresh and challenging look at the fast paced world of science that doesn't shy away from humor and the use of terms and ideas that may require its readers to go online and define a term. And in some cases it may inspire the reader to pick up a book on science by such greats as Hawking and Greene in order to better grasp some of the concepts dealing with time, space and atoms. This magazine isn't dumbed down. It includes well placed and efficient illustrations to help explain some of the more abstract points. It is not designed in the way popular magazinea are, in so much as they only touch upon a topic in the simplest manner and then move on before the audience is lost. Yet this magazine keeps the attention of the reader by combining explanatory notes that help people with no background knowledge have some grasp of the topic and by using humor and well written articles to clearly make their points. 
For a magazine with a serious and well researched list of topics having small cartoons the likes of the New Yorker shows how comfortable this magazine is with itself. From the moment I picked up this magazine for the first time I felt like every word I read mattered and was worth my time to read. (Not true of many other magazines) American Scientist may not have the audience of Discover or National Geographic, nor is it as accessible as said titles, but for those with a true interest in science willing to challenge themselves and commit to real learning this magazine may be a perfect fit. At $4.95 it is certainly worth it to pick a copy on the news stand and try it out." $("#divname").append(html); });

As you can see the segment will be pretty large and I have no way of knowing how big as it is generated dynamically from my database depending on the reviewID which is defined by the user in their request.

The html to be inserted into the div is a list of reviews and is generated using asp.net MVC by a repeater which loops through a list. (if that helps give you an idea of what I am doing).

Is there any way to turn this large segment into one string which can be inserted into the append script?

Thank You

Answer by Click Upvote for Jquery append using multiline


Correct me if i'm wrong but i think everything between the starting and ending quotation marks would be considered part of that string no matter how many lines it has. Unless your string has got any quotation marks in itself, in which case it'll be better to do the equivalent of php's addslashes() function in ASP on your string, which should add a \ before all the " marks in the string.

Another idea can be to use Json to encode/decode the string.

Answer by kgiannakakis for Jquery append using multiline


Some ideas:

  • You can replace new lines with spaces and create a huge line. There shouldn't be a problem with it.
  • Use string concatenation. Split the string and lines and do:

    var html = line1 + line2 + ... linen;

  • Make an Ajax call to fill the div:

    $("#divname").load(service_url);

You need to create a service that will return the string.

In my opinion the 3rd option is better than the other ones.

Answer by Scott Evernden for Jquery append using multiline


i don't see what's wrong with just generating one big-ass long single-line string and appending it just like you are doing. period. done. Fancier isn't going to gain you anything.

Answer by JeremyWeir for Jquery append using multiline


Cross domain jquery json
http://docs.jquery.com/Release:jQuery_1.2/Ajax#Cross-Domain_getJSON_.28using_JSONP.29

Answer by Corey for Jquery append using multiline


Hide it else where on the page and populate the div with it when you need it?


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.