JavaScript Magic Trick: Manipulating URLs

发布时间 2024-01-03 10:41:53作者: w6sft

This article showcases two uncommon JavaScript programming Trick: manipulating browser windows and changing the URLs of parent and child windows.


1.Modifying the URL of the parent window

When using window.open() to open a new window, you can use the window.opener property to access the parent window and modify its URL. For example, suppose page A has the following HTML code:
The code is designed to open page B.html when the link is clicked. In B.html, you can have the following code:
In this case, when you click the link on page A to open page B, A becomes the parent page of B, and B becomes the child page of A. When B is opened, its JavaScript code can modify the URL of A, causing A to navigate to a different website. This achieves the goal of modifying the URL of the parent window from the child page.
This type of JavaScript code is typically not intended to be easily understood by others to protect the implementation logic. You can use JShaman to encrypt the critical JavaScript code. For example:
Above JavaScript code can be encrypted using JShaman JavaScript Obfuscator as:
or as:

or as:


2.Modifying the URL of the child page The code is as follows

Technical principle
After clicking the link, the new page becomes a child page of the current page. The new page opens normally, while the JavaScript code in the current page continues to execute. After 2 seconds, the link address is modified and the link is opened again. Since the target of the link is the same, a new page is not opened, but the previously opened page is displayed. This way, you can modify the URL of the child page in the parent window, which is the opposite of the previous case where the URL of the parent page was modified in the child window.