One of the files causing issues on my website is a Javascript file that is affecting the display specifically on iPad devices using Mobile Safari. I am looking to exclude this file only on iPads and serve it to all other browsers.
Below is the Javascript code I have:
<script type="text/javascript">
<![CDATA[
function removejscssfile(filename, filetype){
var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
var allsuspects=document.getElementsByTagName(targetelement)
for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
}
}
if((navigator.userAgent.match(/iPad/i))
{
removejscssfile("somescript.js", "js") //remove all occurrences of "somescript.js" on the page for iPads
}
]]
</script>
Firstly - I would appreciate feedback on whether the above Javascript code is valid (I have limited knowledge in this area).
Secondly - I am open to suggestions on whether this is the most effective approach to solving this issue.