I'm using XMLHTTP to load the output of an ASP page that contains dynamically created CSS. The CSS modifies existing CSS that is imported to the head section via @import. The ASP page gets data from a MySQL database on a different host.
JavaScript is the only scripting language available in the situation because the pages are part of a host/shopping cart system that uses a mostly undocumented scripting language and database.
Anyway, here's what happens:
OS X FF & Safari: Usually works. Sometimes page loads w/out style modifications, then works on reload. Sometimes page loads w/out style modifications, then turns blank (source code is complete).
IE 6,7,8; FF: Page loads w/out style modifications, or crashes, or turns color of modified body backgroundcolor with source code showing only the imported stylesheet.
Questions:
1) I have the functions and the call to the XMLHTTP function in a file (myajax.inc) which is included in the body of the pages by using the mystery script language's include statement. When I tried splitting the include file up and putting the functions in the head, the page didn't work at all. Why would this be so?
2) The CSS that gets loaded ends up in the body of the page. Does this matter? Is there some way to append the output of the XMLHTTP to the head?
3) Why I am not getting the results I want?
4) Is there a better way of getting the CSS into the page?
Here's the script:
dynamic_css.asp outputs:
JavaScript is the only scripting language available in the situation because the pages are part of a host/shopping cart system that uses a mostly undocumented scripting language and database.
Anyway, here's what happens:
OS X FF & Safari: Usually works. Sometimes page loads w/out style modifications, then works on reload. Sometimes page loads w/out style modifications, then turns blank (source code is complete).
IE 6,7,8; FF: Page loads w/out style modifications, or crashes, or turns color of modified body backgroundcolor with source code showing only the imported stylesheet.
Questions:
1) I have the functions and the call to the XMLHTTP function in a file (myajax.inc) which is included in the body of the pages by using the mystery script language's include statement. When I tried splitting the include file up and putting the functions in the head, the page didn't work at all. Why would this be so?
2) The CSS that gets loaded ends up in the body of the page. Does this matter? Is there some way to append the output of the XMLHTTP to the head?
3) Why I am not getting the results I want?
4) Is there a better way of getting the CSS into the page?
Here's the script:
Code:
<script type="text/javascript">
function getQuerystring(key, default_)
/* from:
[URL unfurl="true"]http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx[/URL]
*/
{
if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}
function ajaxFunction(URL) //from: [URL unfurl="true"]http://www.w3schools.com/[/URL]
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
document.write(xmlhttp.responseText);
}
}
xmlhttp.open("GET",URL,true);
xmlhttp.send(null);
}
var skinid = getQuerystring('skin_id');
var fullurl = "css/dynamic_css.asp?skin_id=" + skinid;
ajaxFunction(fullurl);
</script>
dynamic_css.asp outputs:
Code:
<style type="text/css" media="screen">
[a bunch of styles]
</style>