尝试编写一个 javascript 小书签以从前端链接跳转到 CMS 编辑页面链接。
所以采用这样的 url
http://www.example.com/events/13097/article
然后像这样制作一个 url
http://www.example.com/admin/edit.php?class=events&id=13097
我想我需要使用正则表达式来获取类和 ID,然后将其包装到一个 javascript 函数中——但我是一个绝对的初学者,想知道是否有人可以帮助我入门?
最佳答案
你不需要正则表达式,试试这个:
var url = ""+window.location;
var urlparts = url.split('/');
window.location = "http://www.example.com/admin/edit.php?class="+urlparts[3]+"&id="+urlparts[4];
从 URL 中分离类和 ID,并在重定向中重复它们。
第一行将 window.location
转换为字符串,您也可以使用 String(window.location)
来执行此操作,但这样更冗长。
要获取域名,您可以使用:
"http://"+urlparts[2]+"/admin/edit.php?class="+urlparts[3]+"&id="+urlparts[4]
编辑:实际上,您可以使用 window.location.href.split('/')
获取 urlparts
或模拟原始代码 window.location.toString().split('/')
window.location
object 中其他感兴趣的项目(示例来自 this 帖子)
hash: "#10013173"
host: "stackoverflow.com"
hostname: "stackoverflow.com"
href: "https://stackoverflow.com/questions/10012966/change-url-with-bookmarklet/10013173#10013173"
origin: "http://stackoverflow.com"
pathname: "/questions/10012966/change-url-with-bookmarklet/10013173"
port: ""
protocol: "http:"
search: ""
关于javascript - 使用书签更改网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10012966/