2

我正在尝试获取网站链接,并将其放入添加图像路径的图像源中, 例如: 假设我在一个网站内,我想获取它的链接并将我的图像路径添加到其中并将其注入source image 在同一页面的源代码中。

注意:我使用的是 SharePoint 网站,我们可以使用 HTML 或 JavaScript 吗?

4

3 回答 3

1

您可以从 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-28T08:31:49.283 回答
1

_spPageContextInfo所有 SharePoint 页面上都有一个全局 javascript 变量。它包含有关当前上下文的基本信息。

根据您的图像是存储在 Web 还是网站集级别,您应该使用以下其中一种:

_spPageContextInfo.webAbsoluteUrl
_spPageContextInfo.siteAbsoluteUrl
于 2016-06-29T08:23:37.090 回答
0

要获取网站的位置,请在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-28T08:34:21.703 回答