简体   繁体   English

重定向后JS不起作用

[英]Js not works after redirect

I'm using apppresser to build a WordPress APP and I would like to use a simple JS file to hide menu icon, in one page only.我正在使用 apppresser 构建一个 WordPress APP,我想使用一个简单的 JS 文件来隐藏菜单图标,仅在一个页面中。

When a user logs in, he is redirected to this page.当用户登录时,他将被重定向到此页面。 JS I have written works, but when a user is redirected to this page after login, JS does not seem to be working. JS 我写过作品,但是当用户登录后重定向到此页面时,JS 似乎不起作用。

It seems that after the redirect it loses the page reference.似乎在重定向后它丢失了页面引用。

Useful docs: https://docs.apppresser.com/article/392-custom-javascript有用的文档: https : //docs.apppresser.com/article/392-custom-javascript

I have created a custom html page like this, and assigned a custom ID to div:我创建了一个这样的自定义 html 页面,并为 div 分配了一个自定义 ID:

<img src="https://pathimage.com" onload="disableMenuToggle()" onbeforeunload="enableMenuToggle()" style="height:0px;width:0px; position:absolute;" />
<div id="pianoscaduto">
<button ion-button (click)="openPage('login-modale')">
<ion-icon name="ios-arrow-dropright" item-left></ion-icon>
  FAI IL LOGOUT
</button>
</div>

My JS is:我的JS是:

function disableMenuToggle(){
 var menutoggle = document.getElementsByClassName( 'bar-button-menutoggle' );
if ( menutoggle.length > 0 ) {
        menutoggle[0].disabled = true;
   }
  }

function enableMenuToggle(){
 var menutoggle = document.getElementsByClassName( 'bar-button-menutoggle' );
if ( menutoggle.length > 0 ) {
        menutoggle[0].disabled = false;
   }
  }

I made 2 tries:我做了2次尝试:

When I go to page directly clicking on menu the menu icon is disabled as expected.当我直接点击菜单进入页面时,菜单图标按预期禁用。

But when I get redirected to this page after login, the code does not work.但是当我登录后重定向到此页面时,代码不起作用。

Can the issue be that the redirect is faster and does not have time to load the page?问题可能是重定向速度更快并且没有时间加载页面?

Thanks for help.感谢帮助。

apppresser uses the Angular framework to build a single-page application. apppresser 使用 Angular 框架来构建单页应用程序。 Single page applications "fake" page-loads.单页应用程序“伪造”页面加载。

Your custom JavaScript is being loaded when the app starts, and is run immediately.您的自定义 JavaScript 在应用程序启动时被加载,并立即运行。 When you arrive at your html page after the login page the JavaScript already ran on the login page and is not run again on your HTML page.当您在登录页面之后到达您的 html 页面时,JavaScript 已经在登录页面上运行,并且不会在您的 HTML 页面上再次运行。

Whatever code you put in the custom JavaScript will only be run once, when the app starts.您在自定义 JavaScript 中放入的任何代码都只会在应用程序启动时运行一次。

In your case you can fake an onload by putting a hidden image in your custom HTML page:在您的情况下,您可以通过在自定义 HTML 页面中放置隐藏图像来伪造加载:

<img src="logo.png" onload="window.disableMenuToggle()" onbeforeunload="window.enableMenuToggle()" style="height:0px;width:0px; position:absolute;" />

And declare the functions disableMenuToggle and enableMenuToggle in your custom JavaScript.并在您的自定义 JavaScript 中声明函数disableMenuToggleenableMenuToggle

window.disableMenuToggle = function () {
  var menuToggle = document.getElementsByClassName("bar-button-menutoggle")
  if (menuToggle.length > 0) {
    menuToggle[0].disabled = true;
  }
}
// repeat for enable...

(Img because onload events are only triggered by a select few HTML elements and img is one of them) (Img 因为 onload 事件仅由少数几个 HTML 元素触发,而 img 就是其中之一)

Edit: your custom HTML also won't be "ready" when the JavaScript runs, so for this part don't use the provided ready function, or use it on the body element.编辑:当 JavaScript 运行时,您的自定义 HTML 也不会“准备好”,因此对于这部分,不要使用提供的 ready 函数,也不要在 body 元素上使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM