문제

i've got a mybb board where I provide a certain link, looking like this

<a href="/member.php?action=login&do=switch">Klick</a>

The link changes the session and reloads the page, so I need it executed for the change in the session. However afterwards I would like to redirect the user to another page like "/private.php"

I've already tried different versions of combining href and onclick but it always takes me to the page stated in href, ignoring the onclick bit. So my code looks somewhat like this:

<a href='/member.php?action=login&amp;do=switch&amp;uid={$others['uid']}' onclick='window.location.href({$mybb->settings['bburl']}/private.php);'>Link</a>";

I must say I'm pretty much a noob when it comes to javascript so I would need some help on that. I would like to solve this problem with javascript or php, so no meta tags, please.

도움이 되었습니까?

해결책

You have 2 options:

  • In /member.php?action=login&do=switch, return a redirect response (http 301) to private.php, the browser will automatically redirect after receiving the response.
  • Use ajax to call the link /member.php?action=login&do=switch, and in the callback of the ajax, redirect to private.php using window.location.href = private.php.

An example code using jquery:

$("a").click(function(){
  var href = $(this).attr("href"); //href here is "/member.php?action=login&do=switch"
  $.ajax(
  {
     url:href,
     ...
  }).done(function(){
   window.location.href = "private.php"
  });
  return false; //prevent default action of links.
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top