Saturday, January 22, 2022

How to Get Url from website Using Javascript

                                        


 step 1:  open dilsecodie

 step 2: goto Js section copy code in your function .

step 3: Get Url from the page. check code 

step 4: pass complete URL  in function 

step 5: get the result in the return type 


So common script can be used like :

var url = window.location.href; - its provide browser URL  

var index = url.indexOf("%20"); - to removed encoded keyword

while (index != -1) {  break URL into part 

url = url.replace("%20", " "); - get original URL from  it .

index = url.indexOf("%20"); - get index of word

}

var results = new RegExp('[\\#?&]' + name + '=([^&#]*)').exec(url); - remove unnecessary part

return results[1] || 0; - return your result .

var pagepath = window.location.pathname;

var websitename= window.location.origin; 

also can use these window method to get some results.


So lets see other ways to get URL from browser .

var URL_jq =  $(location).attr('href'); - using jquery method to get URL .

var pagepath_jq =  $(location).attr('pathname'); 

var websitename=  $(location).attr('origin'); 

var portjs = $(location).attr('port');    - get url port  

var protocoljs =$(location).attr('protocol'); - get url using protocol.


$.getJSON("dilsecodie.php?callurl=?", -function used to send request.

 { url:$(location).attr('href')}, - to get url hostname.

function(jsonresult){ }); function used  to perform json responses.

var hosturl = $(this).attr('href');  - using this jquery script can change url parameter

location.hostcode = hostcode; - its will pass current value in it .

Promise.all([ -    fetch data from url in array

  fetch('https://dilsecodie.com/api/').then(req=> req.json()), - send request to direct get url and save       response in array 

  fetch('https://dilsecodie.com/api/').then(req => req.json())

 

]).then(console.log) .

above script used to get data from multiple  URLs and the response will be stored in an array.

To get the current URL use javascript window.location.href property to get complete URL.

for jquery XMLHttpRequest object method used to send URL to backend

document.baseURL its also away to get baseurl from this script


Some javascript property to get URL components.

href - to get complete URL

host - is used to get hostname with port 

protocol - to get URL using protocol

hash - is used to hash portion of URL.

port - is used to get port from URL

search mainly used to get URL query parameter 

Now can see other scripting languge how to get url from browser using php

$_SERVER['QUERY_STRING'] - its used when query parameter with url 

$_SERVER['REQUEST_URI'] - this script used when completed url required


echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];  can used to full path with url


if possible these script didnt work thenyou can use

$_SERVER['HTTP_REFERER']  to get whole url .


for react js if need complete url then for that script  will be

class dilsecodieApplication extends React.PureComponent {

  

  render() {

    this.props.location.pathname - its provide you the pathname of url 


}

then pass this url to component - for thatb  useParam hook to access get values in your url.

and pass that in routes block 

<Route path ="pathfrom url" exact ={} component ={component} />



No comments:

Post a Comment