使用 url 中的 javascript 更改框架属性

2024-02-07

我想根据 URL 使用 javascript 更改 iframe 的大小,

我的意思是,如果网址是:

mywebpage.com/example.html?width=800&height=450

iframe 是

<iframe width="800" height="450" scrolling="no" frameborder="0" allowtransparency="true" marginwidth="0" marginheight="0" src="http://mips.tv/embedplayer/test121/1/800/450"></iframe>

看到 iframe 中的宽度和高度值各是两倍

我这里有一个例子,但它在 iframe 的 src 值中不起作用


“/”字符保留用于定位文件夹。如果使用查询就更好了。

正则表达式对于查询很有用。

//where frame is the iframe you showed above:
var query = JSON.parse(('{'+(frame.source.substr(1).replace(new RegExp("([^=]+)=([^&]+)&?",'g'),function(){return '"'+arguments[1]+'":"'+arguments[2]+'",';}))+'}').replace(/,\}/,"}"));

无论如何,我想您也可以使用正则表达式来实现您想要的格式。我只是strongly强烈建议您使用查询字符串。

var width = -1;
var height = -1;
...
//frame is the iframe that you show.
//You should consider adding an id attribute to that so that you can identify it later.
//It's good coding practice.
var results = frame.match(/([0-9]+)\/([0-9]+)$/);
if(results.length == 3)
{
   width=parseInt(results[1]);
   height=parseInt(results[2]);
}
else
{
   //something went wrong...
}

如果你想改变帧大小,只需调用replace()代替match()。确保您传递了一个函数,如EcmaScript 标准 http://ecma-international.org/ecma-262/5.1/#sec-15.5.1.

要了解有关 JavaScript 中正则表达式的更多信息,请参阅这个网页 http://www.visibone.com/regular-expressions/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 url 中的 javascript 更改框架属性 的相关文章

随机推荐