developer tip

URL / 주소 표시 줄에서 자바 스크립트 함수 호출

copycodes 2020. 8. 18. 07:49
반응형

URL / 주소 표시 줄에서 자바 스크립트 함수 호출


URL에서 자바 스크립트 함수를 호출 할 수 있습니까? 기본적으로 소스에 액세스 할 수없는 페이지에서 JS 메서드를 활용하려고합니다.

다음과 같은 것 : http://www.example.com/mypage.aspx?javascript:printHelloWorld()

javascript:alert("Hello World");주소 표시 줄에 넣으면 작동합니다.

나는 이것에 대한 대답이 아니라고 생각하지만, 그것을 할 방법이 있는지 궁금했습니다.


하이퍼 링크가 아닙니다. 페이지에 특별히 이것에 대한 스크립트가 있고 일부 매개 변수를 확인하지 않는 한 ....하지만 귀하의 질문에 대해서는 브라우저에 내장 된 지원이 없습니다.

그러나 주소 표시 줄에서 JavaScript 기능을 빠르게 실행하기 위해 책갈피를 지정할 수있는 책갈피가 있습니다. 그것이 귀하의 요구를 충족하는지 확실하지 않지만 최대한 가깝습니다.


주소 표시 줄에 쓰기

javascript:alert("hi");

처음에는 javascript :


데이터 URI를 사용할 수 있습니다. 예를 들면 :data:text/html,<script>alert('hi');</script>

자세한 내용은 https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs를 방문하십시오.


/test.html#alert('heello ')

test.html
<button onClick="eval(document.location.hash.substring(1))">do it</button>

다음을 배치 할 수도 있습니다.

<a href='javascript:alert("hello world!");'>Click me</a>

'Click me'하이퍼 링크를 클릭하면 javascript가 url-bar에 나타나고 Alert 대화 상자가 표시됩니다.


다음과 같은 상황을 사용할 수 있습니다. 예를 들어 페이지가 있습니다 : http://www.example.com/page.php그 page.php에 다음 코드를 삽입합니다.

if (!empty($_GET['doaction']) && $_GET['doaction'] == blabla ){
echo '<script>alert("hello");</script>';
}

그런 다음이 URL을 방문 할 때마다 : http://www.example.com/page.php?doaction=blabla

그러면 경고가 자동으로 호출됩니다.


window.location.hash속성 정보 :

URL의 앵커 부분을 반환합니다.


예 1 :

//Assume that the current URL is 

var URL = "http://www.example.com/test.htm#part2";

var x = window.location.hash;

//The result of x will be:

x = "#part2"

예제 2 :

$(function(){   
    setTimeout(function(){
        var id = document.location.hash;
        $(id).click().blur();
    }, 200);
})

예 3 :

var hash = "#search" || window.location.hash;
window.location.hash = hash; 

switch(hash){   
case "#search":  
    selectPanel("pnlSearch");
    break;    
case "#advsearch":    

case "#admin":  

}

Using Eddy's answer worked very well as I had kind of the same problem. Just call your url with the parameters : "www.mypage.html#myAnchor"

Then, in mypage.html :

$(document).ready(function(){
  var hash = window.location.hash;
  if(hash.length > 0){
    // your action with the hash
  }
});

참고URL : https://stackoverflow.com/questions/4163879/call-javascript-function-from-url-address-bar

반응형