我正在使用jQuery.如何获取当前URL的路径并将其分配给变量?
示例网址:
http://localhost/menuname.de?foo=bar&number=0
Run Code Online (Sandbox Code Playgroud) 使用JavaScript从URL中提取路径的正确方法是什么?
示例:
我有URL
http://www.somedomain.com/account/search?filter=a#top
但我只想获得此部分
/帐户/搜索
我正在使用jQuery,如果有任何可以利用的东西.
看起来像window.postMessage的目的是允许在不同域上托管的窗口/框架之间进行安全通信,但实际上它似乎并不允许在Chrome中使用.
这是场景:
src域B *)<iframe>绝对是在域B的上下文中,我已经确认<iframe>中的嵌入式javascript正确执行并postMessage使用正确的值进行调用.
我在Chrome中收到此错误消息:
无法发布消息到一个.收件人有产地乙.
这是在A页面上注册消息事件监听器的代码:
window.addEventListener(
"message",
function (event) {
// Do something
},
false);
Run Code Online (Sandbox Code Playgroud)
我也尝试过调用window.postMessage(some_data, '*'),但所有这一切都是为了抑制错误.
我只是忽略了这一点,是window.postMessage(...)不是为了这个吗?或者我只是做得非常糟糕?
*Mime-type text/html,它必须保留.
可能重复:
使用JavaScript获取当前URL?
你如何在JavaScript中获得你所在页面的地址?
例如,如果我有一个脚本,somesite.com/javascript/home.html我想找到请求地址(somesite.com/javascript/home.html),我如何在JavaScript中获取此信息?
如何让Javascript告诉我网站的网址.
例如,如果我有一个页面 www.example.com/page.html
我想让Javascript告诉我网站的网址是www.example.com不是www.example.com/page.html(哪个document.location告诉我)
有没有办法做到这一点?如果是这样,怎么样?
在此先感谢您的帮助 :)
在具有类别列表的网页上,每个类别标题以此格式链接: http://localhost/admin/category/unpublish/2
我写了下面的js代码,尝试捕获url和段'unpublish'(动作)和'2'(id),并且需要发送请求到 http://localhost/admin/category
$('#statusChanges a').click(function(evt) { // use the click event of hyperlinks
evt.preventDefault();
var url = $(location).attr('href');
// var action = url.segment(3); /*JS console complains that url.segment() method undefined! */
// var id = url.segment(4);
$.ajax({
type: "GET",
url: $(location).attr('href'),
dat: '',
/* do I need to fill the data with json data: {"action": "unpublish, "id": 2 } ? but I don't know how to get the segments */
success: function(data) {
$('.statusSuccess').text('success!');
},
error: function(data) {
$('.statusSuccess').text('error!'); …Run Code Online (Sandbox Code Playgroud) 我想从javascript中的url中提取顶级域名(基本域名).例如,对于下面列出的网址列表,我需要将google.com(或google.co.in视情况而定)作为结果.
www.google.com
www.google.co.in
www.images.google.com
www.images.google.co.in
google.com
google.co.in
images.google.com
images.google.co.in
任何人都知道如何做到这一点.我想没有直接的方法来在javascript中找到基本网址.
我需要根据URL末尾的内容向下滑动叠加层.
如果(URL末尾有'faq'){overlay down down}
你怎么能在jQuery/JavaScript中做到这一点?
有没有办法检测你在JavaScript中的页面?
我想知道的原因是我可以检测到我所在的页面,然后相应地设置元素的样式.
我需要在我的AngularJS应用程序中的#之前提取路径,例如,如果我的应用程序URL是:
http://www.domain.com/folder/app/#/home
Run Code Online (Sandbox Code Playgroud)
我只需要获得这部分内容:
http://www.domain.com/folder/app/
Run Code Online (Sandbox Code Playgroud)
我已经尝试过使用$ location.path,$ location.abspath但找不到任何可以让我获得#之前的第一部分的函数.有人可以通过告诉我在这里缺少什么来帮助我吗?如果有办法获取网址的第一部分(在#之前)?谢谢