javascript - 当 URL 不包含 http 时,Window.Open 会导致问题

标签 javascript window

我有一个文本框,用户可以在其中输入 URL 和名为“检查链接”的按钮。单击按钮后,应打开一个新窗口,其中包含用户输入的 URL。如果我输入网址为“http://google.com”,则 window.open('http://google.com');工作正常,但如果我输入“www.google.com”,那么它会附加到当前窗口网址(http://localhost:1234www.google.com),这不是有效的网址。如何做到这一点?

提前致谢

最佳答案

检查并添加前缀

if (!/^(http:|https:)/i.test(url))
   url = "http://" + url;

关于javascript - 当 URL 不包含 http 时,Window.Open 会导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9717964/

相关文章:

javascript - 在 firestore firebase 中插入并合并特定字段

javascript - 使用 javascript 和 jquery 将图像添加到 svg 文件

javascript - GraphQL/graphcool - 关系的变异

javascript - window.location 或 window.location.href 不更改所有浏览器 MVC 应用程序中的值

javascript - 使用扩展程序移动 chrome 窗口

javascript - react 路由器错误 - 'Cannot call method ' getRouteAtDepth' of undefined'

javascript - jquery - 加载功能在 Chrome 中无法正常工作

python - 如何通过 Python 为 Windows 和 Linux 获取用户登录/注销时间?

c++ - 如何将窗口分为游戏和积分栏

javascript - 如何正确处理在 node-webkit 中打开 _blank 窗口的链接?