簡體   English   中英

我想將我當前的 url 添加到屬性內容的元標記

[英]I want to add my current url to to a meta tag for the attribute content

我想在內容值的元標記中添加我當前的 url,目前使用一些指向錯誤 url 的對象是不正確的。

<meta property="og:url" content="${request.requestURL.toString}" />

這將返回錯誤的 url,此標記位於整個站點的許多其他頁面中使用的組件內,因此它被回收,我不能只對 url 進行硬編碼。

這是我試圖用一些 javascript 做的,但它不起作用,我相信它在知道它在什么 url 之前運行腳本。

<script>

    var currentLocation = window.location;
    </script>
<meta property="og:url" content="${currentLocation}" />




但它不返回當前位置它只是在我查看頁面源時顯示它甚至不顯示內容屬性

<meta property="og:url"/>


在此處輸入圖片說明

如果我檢查,我可以看到 url 在變量中被引用

html 不處理 javascript...它是一種靜態語言。 你需要用腳本設置它

 <meta id="meta-location" property="og:url" content="" /> <script> document.querySelector('#meta-location').setAttribute('content', location.href) </script>

加載元標記后,您必須運行腳本,因為 JS 會在初始化后編輯 DOM。 通過屬性選擇器獲取元數據,如下所示:

<meta property="og:url" content="" />

<script>
  document.querySelector('meta[property]="og:url"').setAttribute('content', location.href)
</script>

或者您可以使用名稱:

<meta name="url" property="og:url" content="" />

<script>
  document.querySelector('meta[name]="url"').setAttribute('content', location.href)
</script>

您可以嘗試這樣的操作,這樣您就可以在元標記中獲得完整的 url。

<meta property="og:url" content="${request.scheme}://${request.serverName}:${request.serverPort}${currentpage.path}.html" />

嘗試將 Java Classes 用於業務邏輯(無論是吊索模型還是擴展 WCMPojo 類)

我有與規范標簽類似的功能。 我所做的是從我的Model返回一個字符串 (Url) 並將其更新為給定條件。

String url;
// getter of url


@PostConstruct
public void afterCreated() {

    url = url + domainName + slingRequest.getRequestURI();
 or
    url = slingRequest.getRequestURL().toString();
 or
    url = "Any modification you like";
}

在 HTML 中

<sly data-sly-use.tagModel="Full class name" >
    <meta property="og:url" content="${tagModel.url}"/>
</sly>

這樣您就可以輕松調試並相應地修改它。

希望這可以幫助。

  1. 使用.querySelector()選擇元標記
  2. 使用.setAttribute()window.location.href (您當前的 URL)設置為元標記的內容
<meta name="url" property="og:url" content="www.sample.com" />
<script>
    document.querySelector("meta[name=\"url\"]").setAttribute("content", window.location.href);
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM