我正在尝试使用此代码从我的网站转到外部网址
var embed = "www.youtube.com";
console.log(embed);
window.location.assign(embed);

但是,网页不会转到var embed中的链接,而是转到 Mywebsite / thepageofthatcode / www.youtube.com
window.location = "www.youtube.com";
window.location.href = "www.youtube.com";

我没理解为什么会这样?
答案 0 :(得分:2)
请确保为外部网站添加http:
window.location.href = "http://www.youtube.com";
答案 1 :(得分:1)
添加协议以使其正常工作。如果没有协议,它将在您的域下搜索www.youtube.com,这就是为什么它会重定向到那种方式。尝试
window.location.href = 'https://www.youtube.com';
答案 2 :(得分:1)
您应该在网址之前使用协议。否则,浏览器会认为它是一条路径。
var embed = "http://www.youtube.com";
console.log(embed);
window.location.assign(embed);
答案 3 :(得分:1)
添加协议以使其正常工作。如果没有该协议,它会认为www.youtube.com是您网站的一部分。
这就是问题所在。但是这里有更好的解决方法:
// Use RegExp to test if embed already has the protocol
// If not, prepend it to embed.
if (!/^https?:\/\//i.test(embed))
embed = 'http://' + embed;
window.location.href = embed;
答案 4 :(得分:1)
这解决了问题,只需要将<div class="align-self-start">Flex item 1</div>
部分添加到网址
undefined