Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีดึงโปรโตคอลและเส้นทางหน้าเว็บปัจจุบันใน JavaScript

location object ให้ข้อมูล URL ปัจจุบัน มี window.location.protocol และ window.location.pathname

Pathname

<html> <body> 
<p id="pathname"></p> 
<script> 
    document.getElementById("pathname").innerHTML = "Page path is: " + window.location.pathname; 
</script> </body> </html>

ผลลัพธ์

Page path is: /cg/assets/jgCOmn.php

Protocol

<html> <body> 
<p id="protoname"></p> 
<script> 
    document.getElementById("protoname").innerHTML = "Protocol name is: " + window.location.protocol; 
</script> </body> </html>

ผลลัพธ์

The page protocol is: https: