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 \ 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 \ S] *?) <\/Script [\ s] *>/gi;
Var matchArray_temp = str_temp.match (regExp_function_temp );
Eval (RegExp. $1 );
}
}
}

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.