itsource

동일한 창과 탭에서 URL 열기

mycopycode 2022. 9. 13. 22:13
반응형

동일한 창과 탭에서 URL 열기

링크가 있는 페이지가 포함된 동일한 창과 탭에서 링크를 엽니다.

를 사용하여 링크를 열려고 할 때window.open그러면 같은 창의 같은 탭이 아닌 새 탭으로 열립니다.

이름 속성을 사용해야 합니다.

window.open("https://www.youraddress.com","_self")

편집: URL 앞에 protocol을 붙여야 합니다.그렇지 않으면 상대 URL을 열려고 합니다.Chrome 59, Firefox 54 및 IE 11에서 테스트 완료.

사용방법:

location.href = "http://example.com";

링크가 같은 탭에서 열리도록 하려면window.location.replace()

다음의 예를 참조해 주세요.

window.location.replace("http://www.w3schools.com");

출처 : http://www.w3schools.com/jsref/met_loc_replace.asp

URL 을 지정하지 않고, 같은 페이지로 이동할 수 있습니다.

window.open('?','_self');

"frame"에 페이지가 있는 경우 "Window.open('logout.aspx'',_self')""

는, 같은 프레임내에서 리다이렉트 됩니다.따라서 를 사용하여

"Window.open('logout.aspx','_top')"

새로운 요청으로 페이지를 로드할 수 있습니다.

Javascript의 가장 중요한 기능 중 하나는 클릭 핸들러를 바로 기동하는 것입니다.사용하는 것보다 다음 메커니즘이 더 신뢰할 수 있다는 것을 알았습니다.location.href=''또는location.reload()또는window.open:

// this function can fire onclick handler for any DOM-Element
function fireClickEvent(element) {
    var evt = new window.MouseEvent('click', {
        view: window,
        bubbles: true,
        cancelable: true
    });

    element.dispatchEvent(evt);
}

// this function will setup a virtual anchor element
// and fire click handler to open new URL in the same room
// it works better than location.href=something or location.reload()
function openNewURLInTheSameWindow(targetURL) {
    var a = document.createElement('a');
    a.href = targetURL;
    fireClickEvent(a);
}

위의 코드는 새로운 탭/창을 열고 모든 팝업 차단기를 바이패스하는 데도 도움이 됩니다!!!예.

function openNewTabOrNewWindow(targetURL) {
    var a = document.createElement('a');
    a.href = targetURL;

    a.target = '_blank'; // now it will open new tab/window and bypass any popup blocker!

    fireClickEvent(a);
}

클릭인 링크와 같은 다른 URL 열기

window.location.href = "http://example.com";

를 사용해야 합니까?window.open? 를 사용하는 것은 어떻습니까?window.location="http://example.com"?

window.open(url, wndname, params), 3개의 인수가 있습니다.동일한 창에서 열지 않으려면 다른 wndname을 설정하십시오.예를 들어 다음과 같습니다.

window.open(url1, "name1", params); // this open one window or tab
window.open(url1, "name2", params); // though url is same, but it'll open in another window(tab).

에 대한 자세한 내용은 다음과 같습니다.window.open()신뢰할 수 있습니다!
https://developer.mozilla.org/en/DOM/window.open

~을 해보다

현재 탭 페이지에서 url 열기_self

const autoOpenAlink = (url = ``) => {
  window.open(url, "open testing page in a same tab page");
}
<a
 href="https://cdn.xgqfrms.xyz/index.html"
 target="_self"
 onclick="autoOpenAlink('https://cdn.xgqfrms.xyz/index.html')">
   open url in the current tab page using `_self`
</a>

새 탭 페이지에서 URL 열기_blank

const autoOpenAlink = (url = ``) => {
  window.open(url, "open testing page in a new tab page");
}

// ❌  The error is caused by a `StackOverflow` limitation
// js:18 Blocked opening 'https://cdn.xgqfrms.xyz/index.html' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.
<a
 href="https://cdn.xgqfrms.xyz/index.html"
 target="_blank"
 onclick="autoOpenAlink('https://cdn.xgqfrms.xyz/index.html')">
   open url in a new tab page using `_blank`
</a>

참조

MDN의 설명서에 따르면 새로운 제품의 이름을 하나만 지정하면 됩니다.window/tab.

https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Syntax

html 5에서는 history API를 사용할 수 있습니다.

history.pushState({
  prevUrl: window.location.href

}, 'Next page', 'http://localhost/x/next_page');
history.go();

그런 다음 다음 페이지에서 다음과 같이 상태 개체에 액세스할 수 있습니다.

let url = history.state.prevUrl;
if (url) {
  console.log('user come from: '+ url)
}

딱 이렇게window.open("www.youraddress.com","_self")

그건 꽤 쉬워요.첫 번째 창을 다음으로 엽니다.window.open(url, <tabNmae>)

예제:window.open("abc.com",'myTab')

그리고 next all window.open에 대해 다음이름 대신_self,_parent기타.

   Just Try in button.

   <button onclick="location.reload();location.href='url_name'" 
   id="myButton" class="btn request-callback" >Explore More</button>

   Using href 

  <a href="#" class="know_how" onclick="location.reload();location.href='url_name'">Know More</a> 

할수있습니다

window.close();
window.open("index.html");

제 웹사이트에서 성공적으로 작동했습니다.

언급URL : https://stackoverflow.com/questions/8454510/open-url-in-same-window-and-in-same-tab

반응형