Reputation: 25
I am adding this to .js file, I should remove Script tag from starting and end? Is there anything else that I should add or remove, so that this works correctly?
<script type="text/javascript">
$(document).ready(function() {
var wid = window.innerWidth;
var hit = window.innerHeight;
//alert( w );
var geturl = window.location.href;
var spliturl = geturl.split("layout=");
if( geturl.indexOf("default") > -1 || spliturl[1] == 'default' ){
var atr = "";
var septer = "";
$("a").each(function(index, element) {
if( $(this).attr("id") != "desktop-view" && $(this).attr("id") != "mobile-view" ) {
atr = $(this).attr("href");
if( atr != undefined) {
if( atr.toString().indexOf("?") > -1) {
septer = "&";
} else {
septer = "?";
}
if( atr.indexOf("#") <= -1 && atr.indexOf("add_to_cart") <= -1) {
$(this).attr("href", atr + septer + "layout=default");
}
}
}
});
var murl = geturl;
murl = murl.replace("&layout=default","");
murl = murl.replace("layout=default","");
$("#mobile-view").attr("href", murl);
if(/iPhone|iPad|iPod|Windows Phone/i.test(navigator.userAgent) ) {
//alert( window.innerHeight + " | " + window.innerWidth );
if( wid == 375 || wid == 667 || (wid == 320 && hit >=400) || wid == 568 || wid == 414 || wid == 736 ) { //|| wid == 414 || wid == 736 added
$("meta[name='viewport']").attr('content','initial-scale=0, minimum-scale=0, maximum-scale=1.0, user-scalable=yes');
//alert("target device");
} else {
$("meta[name='viewport']").attr('content','width=100%, initial-scale=0');
}
/*} else {
$("meta[name='viewport']").attr('content','width=device-width, initial-scale=0');
}*/
} else {
$("meta[name='viewport']").attr('content','width=device-width, initial-scale=0');
}
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
$("#mobile-view").attr("style", "display:block !important");
$("#desktop-view").attr("style", "display:none !important");
}
}else{
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
$("#mobile-view").attr("style", "display:none !important");
$("#desktop-view").attr("style", "display:block !important");
}
var tt='<link href="/assets/css/responsive.css" rel="stylesheet" type="text/css" id="res-css" />';
$("head").append(tt);
$("meta[name='viewport']").attr('content','width=device-width, initial-scale=1.0');
$("a").each(function(index, element) {
if( $(this).attr("id") == "desktop-view" ) {
var vurl = geturl;
if( vurl.indexOf("?") > -1 ) {
vurl = vurl.replace("?","");
}
vurl = vurl.split('?');
//parts = vurl[0].split("/");
vurl = vurl[0] + "?layout=default";
$(this).attr("href", vurl);
}
});
}
});
</script>
Upvotes: 0
Views: 87
Reputation: 1285
You would need to remove the
<script type="text/javascript">
and
</script>
tags, if you want this code in a javascript file. Those script tags are HTML tags which are unnecessary if you have a dedicated javascript file. Of course you then need to ensure you link to the file itself in your HTML, with a line like this:
<script src="/yourownpathhere/javascript.js" type="text/javascript"></script>
Upvotes: 1