错误提示:Uncaught SyntaxError: Illegal return statement

1,837 阅读1分钟
if (m < 0) { //不含有mobile
		// window.location.href = 'hengmobile_index.html';
		console.log('移动端不含有mobile');
		// window.location.href = 'mobile_index.html';
		// window.location.href = '/mobile';
		cur_href = cur_href.replace("gubo/", "gubo/mobile_");
		// cur_href = cur_href.replace("index", "mobile/index");
		window.location.href = cur_href;
	} else {
		console.log('移动端含有mobile');
		return false;
	}


贴出此部分代码,判断时候return false,报错Uncaught SyntaxError: Illegal return statement;查了解决办法,原来很多人碰到,在进行判断时候要把if语句写进函数里,所以写入(function(){......})l或者$(document).ready(function (e) {.....})里就ok了,给自己记一下

(function(){
    ......
    if (m < 0) { //不含有mobile
		// window.location.href = 'hengmobile_index.html';
		console.log('移动端不含有mobile');
		// window.location.href = 'mobile_index.html';
		// window.location.href = '/mobile';
		cur_href = cur_href.replace("gubo/", "gubo/mobile_");
		// cur_href = cur_href.replace("index", "mobile/index");
		window.location.href = cur_href;
	} else {
		console.log('移动端含有mobile');
		return false;
	}
})