Shortcut to Body

Knowledge (Ver English)

To force an address bar URL domain change

To force an address bar URL domain change


When connecting via an external link, you may have to change the domain, such as loading it into an i-frame, or loading ajax with jquery.
For example, ajax has a sandbox security, so there is a limitation that prevents pages from being called from other domains. 
You can solve this problem by forcibly changing the domain and unifying it.

1. The General Method 

The most common use is to declare the path of a document in JavaScript. 
However, in the case of ajax, even this may not work.
But it's the easiest and most common way.  

<script language=javascript> 
document.domain = "www.A.com"; 
</script>

If you do this, even if it is A.com without www, it recognizes it as www.A.com and calls the page.
However, you must read the four precautions at the bottom to avoid failure.     

2. How to match domains 

When the expected address path is obvious, it is more certain to do it as follows. 

<script language=javascript> 
var url1 = 'www.A.com';
var url2 = 'A.com';
if( document.URL.match(url1) ) document.location.href = document.URL.replace(url1, url2); 
</script>

If a visitor wants to access A.com, but he or she wants to access www.A.com, this will force him or her to start with A.com
For example, www.A.com/search?If query=test appears in the URL, force A.com/search?It changes to query=test. 

Enter the desired domain address for url1 and url2.
In the if part, make url2 by unifying url1 and url2.
url1 is the problematic path address and url2 is the desired path address. 

Even if there are various problematic routes such as blog and img as well as www, you can continue to increase it to url3 and url4 above.     


3. Other Advice 

There's JSON and so on.
JSON has a long content, so I'll mention it next time.     

4. Precautions 

However, if you use the first method, document.domain = "Address"; there is a point to note.
Some pages may be useful, but the domain paths within the site are intertwined, which can cause many problems, such as not being saved on other pages.  

Therefore, if document.domain is applied on one page, it is inevitable to apply it on all pages.
In addition, in the case of an eye frame, it is not necessary to apply it only to the parent page, but also to declare document.domain in the document within the called eye frame. 

However, if you use document.URL.match, the second method, you can only declare and use it in the parent window.

[Forced to change the URL domain]



Information in this article


˙ The title of this article: To force an address bar URL domain change
˙ Blog Title: KISS CAST::KISS CAST
˙ Blog Description: 각종 지식 정보를 정리하고 백업하는 문서 블로그입니다. / This is a document blog that organizes and backs up various knowledge information. / 各種知識情報を整理しバックアップする文書ブログです。 / 整理和备份各种知识信息的文档博客。 / Es un blog de documentos que organiza y respalda información de conocimiento.
˙ RSS URL: https://kisscast.tistory.com/rss
˙ View related keywords at once (click)
˙ Error Reporting (Click)