Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将url的第一部分设置为HTML链接中的JavaScript变量_Javascript_Variables_Href - Fatal编程技术网

如何将url的第一部分设置为HTML链接中的JavaScript变量

如何将url的第一部分设置为HTML链接中的JavaScript变量,javascript,variables,href,Javascript,Variables,Href,我有一个变量,用于获取用户的服务器url: var server = parent.Xrm.Page.content.getClientUrl; 我想使用该变量作为html链接的第一部分,并将页面名称添加到其末尾,以生成不同页面的链接。例如,对于主页,我需要a href是var服务器和/homepage的组合。我尝试了以下方法: <a href="" onclick="this.href = 'server'+'/homepage" target="_blank"> 但这不起

我有一个变量,用于获取用户的服务器url:

var server = parent.Xrm.Page.content.getClientUrl;
我想使用该变量作为html链接的第一部分,并将页面名称添加到其末尾,以生成不同页面的链接。例如,对于主页,我需要a href是var服务器和/homepage的组合。我尝试了以下方法:

<a href="" onclick="this.href = 'server'+'/homepage" target="_blank">


但这不起作用。任何解决方案都将不胜感激

您可以编写一个
onclick
方法,该方法将在javascript中实现。在这里,您将获得变量

HTML:

<a href="" onclick="getUrl()" >
getUrl(){
window.open(server +'/homepage,  '_blank');
}
或者,您可以将html中的额外字符串作为参数发送

<a href="" onclick="getUrl('/homepage')">

@UmairKhan我不需要获取网站url。。我已经有了。我需要将其设置为与文本一起的变量。OPs
元素的
target
属性设置为
\u blank
,这将在新窗口/选项卡中打开链接,
window.location=…
将在当前窗口/选项卡中打开链接。
getUrl(url){
window.open(server + url,  '_blank')

}