简体   繁体   English

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

[英]How to set the first part of a url to a JavaScript variable in a HTML a link

I've got a variable that gets the server url of the user:我有一个变量可以获取用户的服务器 url:

var server = parent.Xrm.Page.content.getClientUrl;

I want to use that variable to be the first part of a html link and add the page name to the end of it to generate links for different pages.我想使用该变量作为 html 链接的第一部分,并将页面名称添加到它的末尾以生成不同页面的链接。 For example for the home page I need the a href to be a combination of var server and /homepage.例如对于主页,我需要 a href 是 var server 和 /homepage 的组合。 I tried the following:我尝试了以下方法:

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

But this didn't work.但这没有用。 Any solution would be appreciated!任何解决方案将不胜感激!

You can write an onclick method which will be implemented in javascript.您可以编写一个onclick方法,该方法将在 javascript 中实现。 There you will get the variable.在那里你会得到变量。

HTML: HTML:

<a href="" onclick="getUrl()" >

JS: JS:

getUrl(){
window.open(server +'/homepage,  '_blank');
}

or you can send the extra string from html as parameter或者您可以从 html 发送额外的字符串作为参数

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

- -

getUrl(url){
window.open(server + url,  '_blank')

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM