js 传递的值中间包含变量名称,怎么转化?

window.open(url);

url 是 ajax 取过来的。
内容就是

WEBAPICONSTANT.WEBAPI.URL + '/ssologin/login?m=SBYHMOD_03'

WEBAPICONSTANT.WEBAPI.URL在页面上有定义,但是我绑定的时候直接就指向WEBAPICONSTANT.WEBAPI_SSO.URL%20+%20'/ssologin/login?m=SBYHMOD_03%27 
怎么才能正确的转化?

我的url里面还有 一些页面跳转的方法名,应该怎么写呢?


POPMUISE
浏览 626回答 3
3回答

莫回无

试试这样window.open(eval(WEBAPICONSTANT.WEBAPI.URL) + WEBAPICONSTANT.WEBAPI.URL)

冉冉说

你返回的是字符串并不是js可以执行的代码要做一些转换var WEBAPICONSTANT = {    WEBAPI: {        URL: "1111111111"    }}var str = "WEBAPICONSTANT.WEBAPI.URL + '/ssologin/login?m=SBYHMOD_03'";console.log(str)//WEBAPICONSTANT.WEBAPI.URL + '/ssologin/login?m=SBYHMOD_03'//理由eval转换console.log(eval(str))//1111111111/ssologin/login?m=SBYHMOD_03//理由new Function转换var func = new Function("return " + str);console.log(func())//1111111111/ssologin/login?m=SBYHMOD_03不过一般都是这样var WEBAPICONSTANT = {    a:"aaaa",    b:"bbbb"}var str = "a";console.log(WEBAPICONSTANT[str])//aaaa

弑天下

<script>&nbsp; &nbsp; var WEBAPICONSTANT = {&nbsp; &nbsp; &nbsp; &nbsp; WEBAPI: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URL: 'test'&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var getName = 'WEBAPICONSTANT.WEBAPI.URL';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; console.log(eval(getName)); // test&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; //当然,还可以这样&nbsp; &nbsp; function abc (str) {&nbsp; &nbsp; &nbsp; &nbsp; str = str.split('.');&nbsp; &nbsp; &nbsp; &nbsp; var result = window;&nbsp; &nbsp; &nbsp; &nbsp; for(var a=0;a<str.length; a++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = result[str[a]];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; console.log(abc(getName)); // test</script>上面都说了两种了,我再来一种
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript