Home > Article > Web Front-end > How to implement refresh-free jump between two iframe pages in Vue
As a front-end engineer, we often encounter situations where we need to jump between two different web pages. In the Vue framework, the Single Page Application (SPA) model it advocates requires different ideas and methods when making page jumps. So, how to implement refresh-free jump between two iframe pages in Vue? This article will discuss and demonstrate this issue.
1. Jump using window object
The simplest way to jump is to use the location.href attribute of the window object, for example:
window.location.href = 'http://www.example.com';
This method is suitable for ordinary Jump to a web page, or jump to a page in a single iframe. However, when we need to jump to another iframe, we need to first obtain the window object of the iframe and then jump on it. Suppose we now have two nested iframes. The code is as follows:
<iframe id="iframe1" src="http://www.example1.com"></iframe> <iframe id="iframe2" src="http://www.example2.com"></iframe>
We need to jump to the inside of a button in the first iframe by getting the window object of the second iframe. A certain page and achieve no refresh. The specific implementation code is as follows:
// 获取 iframe2 的 window 对象 var iframe2Window = document.getElementById('iframe2').contentWindow; // 跳转到 iframe2 中的一个页面,无刷新 iframe2Window.location.href = 'http://www.example2.com/page1.html';
The advantage of this method is that it is simple and easy to implement, and it can also achieve cross-iframe jumps. But its shortcoming is also very obvious, that is, it cannot realize cross-domain jumps between Vue components.
2. Use Vue global instance to jump
In Vue, we can jump to the page by accessing the global Vue instance. The specific method is to call the router object through the $router attribute to achieve a refresh-free jump. Assuming that we have instantiated the router through VueRouter, then we can implement routing jump in the following way:
// 跳转到指定路径,无刷新 this.$router.push('/path/to/your/page')
The advantage of this method is that it can achieve better adaptability and controllability, and at the same time Strictly adhere to the design principles of Vue single-page applications. However, when using a Vue instance to jump in an iframe, page refresh will still occur, and refresh-free jump cannot be achieved.
3. Use postMessage to pass values across domains
When we need to jump between the parent page iframe and the child page iframe, we usually encounter cross-domain problems. At this time, the best solution is to use the HTML5 API-postMessage. This API allows us to transfer values between different windows to achieve cross-domain jumps. The specific implementation steps are as follows:
Send postMessage in the parent page:
// 该消息为跳转信息,detail为目标跳转路径 window.frames[0].postMessage({ type: 'jump', detail: '/path/to/your/page' }, '*');
Listen to the message in the child page:
window.addEventListener('message', function (msg) { if (msg.data.type === 'jump') { // 将子页面重定向到目标页面 window.location.replace(msg.data.detail); } }, false);
The advantage of this method is that it can achieve cross- The field jumps with parameters, which facilitates us to jump to the page. But its disadvantage is also obvious, that is, it is difficult to maintain.
To sum up, when we jump to the iframe page, we choose different implementation methods according to the specific situation. Of course, in the Vue framework, we recommend the second method - using the $router attribute to jump, and it is also feasible to implement it across iframes.
The above is the detailed content of How to implement refresh-free jump between two iframe pages in Vue. For more information, please follow other related articles on the PHP Chinese website!