Methods for embedding a large number of string constants in JavaScript

Source: Internet
Author: User

"Go" embedding a large number of string constants in a JavaScript file is something that is often encountered. Sometimes for the sake of convenience, some interface HTML and CSS directly written in the JS file. Small number of good, more words on a lump of text, pay attention to the artistic youth, will use a lot of character connection symbols even with indentation, forced into several lines. For example:

 var html = '  <  div  >   ' + '  <  p  >  hello</ p  >   ' + '  <  >  World ' </ p  >   "+ '  </ div  >  '; 

This is OK, if there are a lot of double quotation marks in the string, it is more troublesome, a variety of escape characters to see the dazzling.

In fact, there is a little small skill, can solve the problem. People always think that the string must be in the "..." or "...", that's right. But there is also a place where the string can also be preserved as non-code semantics, which is a function of the ToString, the entire function of the code when the string output-the annotation part of course is also reserved!

So we write an empty function, which is a/**/comment, which is the constant content we want. After ToString, add a regular to extract what we want!

Try it now:

varRes_code = _text (function(){/*#include <iostream> int main () {std::cout << "Hello World" << Std::endl;    return 0; }*/});varRes_poem = _text (function(){/*more blows, the stars as rain.  BMW Carving Car Incense full road.  Feng Xiao Sound movement, jade pot light turn, one night fish dragon dance.   Moth son snow willow golden wisp, laughter and dark incense go. The crowd found him 1100 degrees, suddenly look back, the man is in the dim place. */});varRes_xml = _text (function(){/*<projectDescription> <name>Hello</name> <comment></comment> < ;p rojects> </projects> <buildSpec> <buildCommand> <name>             ;com.adobe.flexbuilder.project.flexbuilder</name> <arguments> </arguments> </buildCommand> <buildCommand> <name>com.adobe.flexbuilder.project .apollobuilder</name> <arguments> </arguments> </buildcomman d> </buildSpec> </projectDescription>*/});function_text (Wrap) {returnWrap.tostring (). Match (/\/\*\s ([\s\s]*) \s\*\//) [1];} alert (res_code); alert (res_poem); alert (res_xml) ;

Methods for embedding a large number of string constants in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.