我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
基本上,jQuery只是一个JavaScript框架,在这种情况下,你只能使用纯粹的JavaScript,所以在这种情况下,你有3个选项使用VanillaJavaScript:
使用位置替换,这将取代当前页面历史,这意味着无法使用后按钮返回原始页面。
window.location.replace("http://stackoverflow.com");
2) 使用位置指定,这将为您保存历史,并使用返回按钮,您可以返回原始页面:
window.location.assign("http://stackoverflow.com");
3)我建议使用上述方式之一,但这可能是使用纯粹的JavaScript的第三个选项:
window.location.href="http://stackoverflow.com";
您也可以在 jQuery 中编写一个函数来处理它,但不推荐,因为它只是一个线纯的JavaScript函数,如果您已经在窗口范围内,也可以使用上面的所有函数没有窗口,例如 window.location.replace(“http://stackoverflow.com”);可以是 location.replace(“http://stackoverflow.com”);
我在下面的图像中展示了所有这些:
此分類上一篇
其他回答
使用 jQuery 功能:
$.extend({
redirectPost: function(location, args) {
var form = '';
$.each(args, function(key, value) {
form += '<input type="hidden" name="' + key + '" value="' + value + '">';
});
$('<form action="' + location + '" method="POST">' + form + '</form>').appendTo($(document.body)).submit();
}
});
在你的代码中,你使用它如下:
$.redirectPost("addPhotos.php", {pimreference: $("#pimreference").val(), tag: $("#tag").val()});
JavaScript 是非常广泛的. 如果你想跳到另一个页面,你有三个选项。
window.location.href='otherpage.com';
window.location.assign('otherpage.com');
//and...
window.location.replace('otherpage.com');
当你想移动到另一个页面,你可以使用其中一个,如果这是你的要求. 但是,所有三个选项都限于不同的情况。
如果你有兴趣了解更多关于这个概念,你可以通过更多。
window.location.href; // Returns the href (URL) of the current page
window.location.hostname; // Returns the domain name of the web host
window.location.pathname; // Returns the path and filename of the current page
window.location.protocol; // Returns the web protocol used (http: or https:)
window.location.assign; // Loads a new document
window.location.replace; // RReplace the current location with new one.
var url = 'asdf.html';
window.location.href = url;
我已经使用JavaScript的重定向功能(),它工作了。
<script type="text/javascript">
$(function () {
//It's similar to HTTP redirect
window.location.replace("http://www.Technomark.in");
//It's similar to clicking on a link
window.location.href = "Http://www.Technomark.in";
})
</script>
location.assign(): 指定路径通过路径到它.. 指定将给你一个历史,即使路径被分配后. 使用方法:值应该转移到它. 例如: location.assign(“http://google.com”) location.href 可以定义给一个路径到它... 它将重定向到一个特定的路径一旦它设置,它将保持历史... 使用方法:值应该分配到它
assign() 和 href 是相似的,两者都可以持有历史。 assign 将通过通过值工作, href 通过分配工作。
您可以使用 JavaScript 本身实现,而不使用 jQuery 通过分配,
window.location = "http://google.com"
location.href = "http://google.com"
你可以得到类似的事情使用jQuery如下。
$(window).attr('location', "http://www.google.com");
$(location).attr('href', "http://www.google.com");
你可以很容易地理解两者之间的差异。
这里是位置对象,
此分類上一篇
推荐文章
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 登录后,Laravel重定向回原始目的地
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- 用jQuery检查Internet连接是否存在?
- 如何使用滑动(或显示)函数在一个表行?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类