Use regular expressions to dynamically create/Add CSS style scripts compatible with IE Firefox

Source: Internet
Author: User

STR indicates the xmlrequest returned through Ajax
/* Dynamically load CSS style */
Function loadstyle (STR)
{
VaR regexp_src =/<style. * SRC \ s * = \ s * ["'] [^"'] * ["']. *> [^ <>] * <\/style \ s *>/GI;
VaR matcharray_src = Str. Match (regexp_src );
Alert (matcharray_src [0]);
If (matcharray_src ){
For (VAR I = 0; I <matcharray_src.length; I ++)
{Alert ("Sss ");
VaR str_temp = matcharray_src [I]. tostring ();
VaR regexp_src_temp =/<style. * SRC \ s * = \ s * ["'] ([^"'] *) ["']. *> [^ <>] * <\/style \ s *>/GI;
Str_temp.match (regexp_src_temp);/* here, the matching is used repeatedly. The $1 subexpression points to the current value, otherwise it will always be the last $1 value */
VaR head = Document. getelementsbytagname ('head') [0];
/* Ie */
If (document. All ){
Alert ("ie ");
If (document. getelementsbytagname ('style') [0])
{
VaR sty = Document. getelementsbytagname ('style') [0]. innerhtml;
Alert (STY );
VaR sty = Document. getelementsbytagname ('style') [0]. innerhtml;
// I don't know how to obtain the current style label of IE. If you use getelementsbytagname, you can only use this policy to create a new one and add the previous style.
VaR stylesheet = Document. createstylesheet ();
Stylesheet.css text = sty + Regexp. $1;
}
Else
{
VaR stylesheet = Document. createstylesheet ();
Stylesheet.css text = "body {Background: Red ;}\ <br \>"
Alert ("finished ");
}
}
/* Firefox */
Else {
VaR style;
If (document. getelementsbytagname ('style') [0])
{
VaR sty = Document. getelementsbytagname ('style') [0]. innerhtml;
Alert (STY );
Document. getelementsbytagname ('style') [0]. innerhtml = sty + Regexp. $1;
}
Else
Style = Document. createelement ('style ');
Style. type = 'text/CSS ';
Style. innerhtml = Regexp. $1;
Head. appendchild (style );
Alert (Regexp. $1 );
}
}
}
}
/* Dynamically load a script with the src attribute */
Function loadscript_src (STR ){
VaR regexp_src =/<script. * SRC \ s * = \ s * ["'] [^"'] * ["']. *> [^ <>] * <\/script \ s *>/GI;
VaR matcharray_src = Str. Match (regexp_src );
If (matcharray_src)
{
For (VAR I = 0; I <matcharray_src.length; I ++)
{
VaR str_temp = matcharray_src [I]. tostring ();
VaR regexp_src_temp =/<script. * SRC \ s * = \ s * ["'] ([^"'] *) ["']. *> [^ <>] * <\/script \ s *>/GI;
Str_temp.match (regexp_src_temp );
VaR head = Document. getelementsbytagname ('head') [0];
VaR script = Document. createelement ('script ');
Script. type = 'text/JavaScript ';
Script. src = Regexp. $1;
Script. Defer = "true ";
Head. appendchild (SCRIPT );
Alert (Regexp. $1 );
}
}
}
/* Dynamically load the script in innerhtml */
Function loadscript (STR ){
VaR regexp_function =/<SCRIPT [^>] *> ([\ s] *?) <\/Script [\ s] *>/GI;
VaR matcharray_function = Str. Match (regexp_function );
If (matcharray_function ){
For (VAR I = 0; I <matcharray_function.length; I ++ ){
VaR str_temp = matcharray_function [I]. tostring (); // In fact, it is very convenient to search forward and backward using a regular expression, but JavaScript does not support it. Therefore, this policy can only be used.
VaR regexp_function_temp =/<SCRIPT [^>] *> ([\ s] *?) <\/Script [\ s] *>/GI;
VaR matcharray_temp = str_temp.match (regexp_function_temp );
Eval (Regexp. $1 );
}
}
}
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.