gpt4 book ai didi

javascript - Rails javascript window.location 不工作

转载 作者:行者123 更新时间:2023-12-03 12:12:32 25 4
gpt4 key购买 nike

我正在尝试使用 Javascript (Coffeescript) 路由到 Rails 中的不同页面。

这是我的代码:

  # go to edit page
serverused = window.location.host
newurl = serverused + '/sites/' + node.id + '/edit'
alert newurl
window.location = newurl
alert window.location
return

提醒新网址 = "localhost:3000/sites/2/edit"

警报窗口.location (甚至不应该去)显示:“localhost:3000/stites/stites/tree#node -5” - 是当前的URL。

感谢您的帮助!

最佳答案

您的 newurl 可能需要在其前面添加 http://https:// 。或者您可以从当前位置获取协议(protocol):

newurl = window.location.protocol + '//' + serverused + '/sites/' + node.id + '/edit'

您还可以在 Coffeescript 中使用字符串插值(需要双引号):

newurl = "#{window.location.protocol}//#{serverused}/sites/#{node.id}/edit"

关于javascript - Rails javascript window.location 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24892640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com