Pergunta

I have links in my footer that I want linking to particular sections of a slide i have built on my site I am currently linking the footer links to these sections via

 <a href="javascript:showAndScroll(1, 1)">LINK!</a>

Which works on the page that has the content slide. However I also want these links to work from other webpages. I came up with the solution below to load the "showandscroll" after its been redirected to the page/anchor of the slider

<a id="sem-seo" nohref title="">LINK!</a>

$("#sem-seo").click(function() {
window.location.assign("portfolio.html#anchor2");
$(window).bind("load", showAndScroll(1, 2));
});

But for some reason (loads the page fine and goes to the section) it just does not load the showAndScroll Query

Any thoughts? Ive done hours of research and created a few questions regarding this today but needing the end of a deadline so need a solution.

here is a of the landing page (I want to be able to link to the different sections from other pages) Fiddle

Foi útil?

Solução

Your issue is your code will never execute.

$("#sem-seo").click(function() {
//THIS LINE CAUSES A NEW PAGE TO LOAD
window.location.assign("portfolio.html#anchor2");
//THAT MEANS THIS CODE WILL NEVER BE REACHED
$(window).bind("load", showAndScroll(1, 2));
});

You need to apply your code on the destination page, you can't execute javascript code for any page except the one you are on.

Also, don't attach handlers within handlers. If someone repeatedly clicks on #sem-seo, you will attach multiple load handlers to your window, which is not what you want.

UPDATE

I think this will resolve your issue. On your origin page, add this to the link:

 <a id="sem-seo" href="portfolio.html#anchor2?show=true" title="">LINK!</a>

And on your destination page, add this script:

  $(function(){
       var url = window.location.href;
       if(url.indexOf('?show=true') != -1 || url.indexOf('&show=true') != -1)
            $(window).bind("load", showAndScroll(1, 2));
  });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top