如何将iFrame的URL的一部分更改为随机字符串? [英] How to change part of an iFrame’s URL to a random string?

查看:150
本文介绍了如何将iFrame的URL的一部分更改为随机字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个简单的iFrame:

Consider this simple iFrame:

<iframe src="http://example.com/iframe/?user=123&name=test"
  width="200" height="200"></iframe>

我想要的是随机更改零件 test

What I want is to randomly change the part test.

例如更改此

src="http://example.com/iframe/?user=123&name=test"

是这样的:

src="http://example.com/iframe/?user=123&name=yxcvb"

该字符串应包含字母的随机字母长度最多为10个字符,例如:

The string should contain random letters of the alphabet and have a length of up to 10 characters, e.g.:

src="http://example.com/iframe/?user=123&name=qwertzuiop"


推荐答案

我相信这对您有用:
随机字母数字JavaScript中的字符串?

只需生成一个随机的st响铃(1到10个字符之间),然后将其分配给iframe的name属性。

Just generate a random string (between 1 and 10 characters), and then assign it to the name attribute of the iframe.

我为你拼凑了一个小提琴: http://jsfiddle.net/4agLwfe8/

I've put together a fiddle for you: http://jsfiddle.net/4agLwfe8/.

<iframe id="iframe" src="http://www.facebook.com?user=123&name=test" width="200" height="200">

此解决方案假设您没有使用JQuery,因为您说您不确定该怎么做无论如何,这是一个简单的JavaScript解决方案。

This solution assumes you aren't using JQuery, since you said you weren't sure how to do this anyway, so it's a simply JavaScript solution.

首先,你有一个简单的iframe。我只是用facebook作为一个简单的例子。我还添加了一个ID。

To start you have a simple iframe. I just used facebook as a quick example. I also added an ID to it.

然后我使用了以下JavaScript

Then I used the following JavaScript

function randomNumber(){
    return Math.floor(Math.random() * 10) + 1;  
}

function randomString(length, chars) {
    var result = '';
    for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
    return result;
}

var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var iframe = document.getElementById("iframe");
var iframeSrc = iframe.src;
var newIframeSrc = iframe.src.substring(0,iframe.src.indexOf("name")) + "name=" + randomString(randomNumber(), chars);
iframe.src = newIframeSrc;

首先有2个功能。第一个生成1到10之间的随机数,这将是查询字符串中name参数的字符长度。第二个函数生成此随机字符串,传入随机数而不是生成的字符串,以及您指定可以使用的字符串(来自我链接到的初始解决方案)。

First there are 2 functions. The first generates a random number between 1 and 10, this will be the length of the characters for your "name" parameter in your query string. The second function generates this random string, passing in the random number than was generated, and the string of characters (from the initial solution I linkted to) you specified you could use.

从那里,我只是通过我给它的ID选择iframe并抓住它的来源。使用源代码,我只是简单地删除name参数及其子字符串的值,然后附加新的name参数并重新使用它的新值。最后,我将iframe的新来源分配给iframe。事后我意识到我可以避免砍掉name =只是为了重新加上它,但是我开始在早上6:30看到这个,然后我的大脑完全清醒了。 :)

From there, I am simply selecting the iframe by the ID I gave it and grabbed it's source. With the source, I am simply cutting out the "name" parameter and it's value with the substring, and then appending the new "name" parameter and it's new value back on. Finally I'm assigning the new source for the iframe back to the iframe. In hindsight I realized I could have avoided chopping off "name=" only to add it back on again, but I started looking at this at 6:30am before my brain was fully awake. :)

这篇关于如何将iFrame的URL的一部分更改为随机字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆