如何创建Escape按钮

How to create an Escape Button

本文关键字:Escape 按钮 创建 何创建      更新时间:2023-09-26

我有一个网站需要一个escape按钮,它的工作原理与此类似(第页左侧)

我试图了解他们是如何编写代码的,但找不到按钮引用的JavaScript或jQuery文件。

当你点击浮动按钮时,它会将你发送到另一个网站,然后禁用后退按钮并从你的历史记录中删除该网站。

不幸的是,对于如何开始写这个剧本一无所知。

在Chrome开发者控制台中,您可以发现他们的函数名为escape_page。在javascript控制台中键入"escape_page",函数显示如下:

function escape_page() {
//here we will first set an array of site withc we can use in the function
var safeSites = [ 
    "http://google.com", 
    "http://facebook.com",
    "http://idahostatesman.com",
    "http://youtube.com",
    "http://groupon.com",
    "http://livingsocial.com", 
    "http://cnn.com",
    "http://twitter.com",
    "http://yahoo.com",
    "http://bing.com",
    "http://digg.com",
    "http://reddit.com"
];
var randSitesArray = randomizeArray(safeSites);
var randSite = randSitesArray[Math.floor(Math.random()*safeSites.length)];
for (var i = 0; i < randSitesArray.length; i++) {
    // for each array items fill history
    //History.pushState(null, "Title", randSitesArray[i]);
    History.replaceState({state:i}, randSitesArray[i], null);
    History.pushState({state:i}, randSitesArray[i], null); 
}
//  redirect to one of the websites witht he randomized array
//alert(randSite);
window.location.replace(randSite);
}

然而,我在他们的网站上没有看到任何许可免责声明,所以不要复制粘贴他们的代码。

您可以在任何可点击的元素上使用location.replace方法。仅使用javaScript:

<input id="escape_button" type="button" value="escape" onClick="window.location.replace('http://www.google.com')" />

来自w3schools:

replace()方法将当前文档替换为新文档。

此方法与assign()的区别在于,replace()从文档历史记录中删除当前文档的URL,这意味着无法使用"返回"按钮导航回原始文档。