I am attempting to implement two separate stylesheets on my WordPress blog - one for web access and another for our iOS app. Currently, we are adding ?app=true to the URL when accessing content through the app in order to distinguish between the two. Using a JSON API plugin, the app pulls blog posts to display them within a web view interface.
Within my WordPress blog, I have integrated JavaScript that detects the presence of ?app=true in the URL to dynamically load the appropriate stylesheet.
<script language="javascript" type="text/javascript">
var location = window.location.href;
if(location.indexOf("?app=true") > -1) {
document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"appstyle.css\" />");
}
</script>
This script is included in the header.php file of my WordPress theme.
The stylesheet I want to use, named appstyle.css, resides in the same directory as header.php.
However, there is an issue with the code causing the browser page to reload indefinitely. This problem seems related to the document.write function, since removing it resolves the issue (although the appstyle.css is not loaded).
I also attempted the following if statement:
if(window.location.search.indexOf('?app=true') === 0)
but it did not yield any different results.
Are there more effective methods for loading distinct stylesheets based on whether they are accessed via the iOS app or web? Could there be an error in my implementation?