我正在尝试获取网站链接,并将其放入一个添加图像路径的图像源中,例如:示例:假设我在一个网站中,我希望获得它的链接,并将我的图像路径添加到其中,并将其插入到同一页源代码中的源图像中。
注意:我使用的是SharePoint站点,我们可以使用JavaScript或HTML吗?
发布于 2016-06-28 08:34:21
要获得该网站的位置,请用js编写以下内容
window.location.href //gives complete href path
要将其添加到图像中,可以通过以下方式将图像附加到html中:
var elem = document.createElement("img");
elem.src = window.location.href+"/my-image.png";
elem.setAttribute("height", "200");
elem.setAttribute("width", "200");
elem.setAttribute("alt", "my image");
document.querySelector("#image-container").appendChild(elem);
最新答覆第2号:
因为它是您想要的基本路径,您可以硬编码它,因为它永远不会改变。
通过以下方式将图像附加到html:
var fullPath = window.location.href;
var baseDomain = fullPath.split('site1')[0];
var pngPath = '_catalogs/masterpage/images/banner1.png';
var elem = document.createElement("img");
elem.src = baseDomain + pngPath;
elem.setAttribute("height", "200");
elem.setAttribute("width", "200");
elem.setAttribute("alt", "Banner");
document.querySelector("#banner").appendChild(elem);
发布于 2016-06-28 08:31:49
您可以从javascript获得网站地址。使用下面的代码。
window.location.href : Complete link
window.location.host : you'll get sub.domain.com:8080 or sub.domain.com:80.
window.location.hostname : you'll get sub.domain.com.
window.location.protocol : you'll get http:
window.location.port : you'll get 8080 or 80.
还有为window.location
对象公开的其他属性。
发布于 2016-06-29 08:23:37
在所有的_spPageContextInfo
页面上都有一个全局javascript变量SharePoint可用。它包含有关当前上下文的基本信息。
根据您的图像是否存储在网站或网站集合级别上,您应该使用其中之一:
_spPageContextInfo.webAbsoluteUrl
_spPageContextInfo.siteAbsoluteUrl
https://stackoverflow.com/questions/38070946
复制相似问题