Home > Web Front-end > JS Tutorial > Use regular expressions to dynamically create/add css style script compatible with IE firefox_javascript tips

Use regular expressions to dynamically create/add css style script compatible with IE firefox_javascript tips

WBOY
Release: 2016-05-16 18:55:29
Original
1302 people have browsed it

str represents the XMLRequest returned through ajax
/*Dynamic loading css style*/
function loadStyle(str)
{
var regExp_src=/[^]**>/gi;
var matchArray_src=str.match(regExp_src);
alert(matchArray_src[0]);
if(matchArray_src){
for(var i=0;i{alert("sss");
var str_temp = matchArray_src[i].toString();
var regExp_src_temp = /[^]**> /gi;
str_temp.match(regExp_src_temp);/*The reason for repeated use of matching here is that 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 get the current style tag of IE. I can’t use getElementsByTagName. I can only adopt this strategy, create a new one and add the previous style
var styleSheet = document.createStyleSheet();
styleSheet.cssText=sty RegExp.$1;
}
else
{
var styleSheet = document.createStyleSheet();
styleSheet.cssText=" body{background:red;}
"
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);
}
}
}
}
/*Dynamic loading of script with src attribute*/
function loadScript_src(str){
var regExp_src=/[^]**>/gi;
var matchArray_src=str.match(regExp_src);
if(matchArray_src)
{
for(var i=0;i{
var str_temp = matchArray_src [i].toString();
var regExp_src_temp = /[^]**>/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);
}
}
}
/*Dynamic loading of innerHTML Script*/
function loadScript(str){
var regExp_function=/<script>]*>([sS]*?)</script>[s]*>/gi;
var matchArray_function=str.match(regExp_function );
if(matchArray_function){
for(var i=0;ivar str_temp=matchArray_function[i].toString();//It is actually a regular expression It is very convenient to search forward and backward, but javascript does not support it, so we can only take this last resort
var regExp_function_temp=/<script>]*>([sS]*?)</script>[s]*>/gi ;
var matchArray_temp=str_temp.match(regExp_function_temp);
eval(RegExp.$1);
}
}
}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template