如何在.js文件

时间:2015-08-14 10:43:47

标签: javascript html tags

我将此添加到.js文件中,我应该从开始和结束中删除脚本标记? 还有什么我应该添加或删除,以便这可以正常工作吗?



 <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>
&#13;
&#13;
&#13; 这段代码从.html文件包含到另一个.html中,我想在.js文件中添加它,而不是从.html文件中包含它。 如果有任何不清楚的地方,请先询问问题。

1 个答案:

答案 0 :(得分:1)

您需要删除

<script type="text/javascript">

</script>

标签,如果您想在javascript文件中使用此代码。这些脚本标记是HTML标记,如果您有专用的javascript文件,则不需要这些标记。当然,您需要确保在HTML中链接到文件本身,如下所示:

<script src="/yourownpathhere/javascript.js" type="text/javascript"></script>