Jump to content

What is the best way to redirect an existing page without disabling AJAX?

Recommended Posts

<script>
window.Squarespace.onInitialize(Y, function() {
location.assign("https://www.squarespace.com");   
}
);
</script>

If I inject this code into the header of a page, shouldn't the browser be directed to squarespace.com...? 

Nothing happens... Any ideas why?   Is my code wrong? 

I'm more of a designer than a developer, but I know some HTML, CSS, and Javascript, and PHP, but I would not describe myself as good at any of them. I know just enough to get things to work the way I want with a little help sometimes, even if it takes hours and hours of burning time.

Link to comment
  • Replies 5
  • Created
  • Last Reply

I should mention that I am doing this in a specific "Page Header Code Injection" but when I load the page and view source... I don't even see this code... I have definitely saved the page. Has page header code injection been removed from SquareSpace but the menu option still exists or something? 

 

I'm more of a designer than a developer, but I know some HTML, CSS, and Javascript, and PHP, but I would not describe myself as good at any of them. I know just enough to get things to work the way I want with a little help sometimes, even if it takes hours and hours of burning time.

Link to comment

So what I have gathered is that I actually need to so some kind of sitewide code injection to set up a listener for when there is any kind of an AJAX refresh which will reinitialize the function... 
The problem is that the function I want is a page redirect... 
So I understand THIS code is what I would want as part of a SITEWIDE code injection... 
continued below... 

<script>
(function() {
  // Establish a function that does stuff.
  var myFunction = function() {
    // Do stuff here.
  };

  // Initialize the fn on site load.
  myFunction();
  // myFunction2(); , etc...

  // Reinit. the fn on each new AJAX-loaded page.
  window.addEventListener("mercury:load", myFunction);
})();
</script>

But what I am NOT sure about as I am in no way any kind of javascript expert... (or even novice) 

is what myFunction should be if I am going to be doing a different page redirect by doing "Page Header Code Injection"  on individual pages. 
I have come to understand an effective way to do a page redirection might be
 

<script>
         setTimeout(function(){
            window.location.href = 'https://example.com';
         }, 3);
</script>

but I would NOT want to perform a redirect on the initial site load (since the listener looking for mercury load would be on every page) 

Any help here is greatly appreciated. 

 

I'm more of a designer than a developer, but I know some HTML, CSS, and Javascript, and PHP, but I would not describe myself as good at any of them. I know just enough to get things to work the way I want with a little help sometimes, even if it takes hours and hours of burning time.

Link to comment

Something like window.location won't work because of the squarespace AJAX mercury loader not being coded to check for new javascript when there is a page change and I am not a javascript expert but with a little help might be able to implement something... 

Is there a way to set up a site-wide event listener so that I can trigger an individual page to redirect via either a page specific header injection or an event in a code block?  

I'm more of a designer than a developer, but I know some HTML, CSS, and Javascript, and PHP, but I would not describe myself as good at any of them. I know just enough to get things to work the way I want with a little help sometimes, even if it takes hours and hours of burning time.

Link to comment

My understanding is that I cannot simply insert code into the header or a code block because my squarespace 7.0 template (jasper jin) uses AJAX and so does not reload the page. I like the AJAX, I don't want to disable it.

How do I set up an event listener or something to make it so that a code block or header script tags WILL be fired when that part of the page loads? I don't want to disable ajax and I'm not an javascript expert but this seems like it should be simple code if I can get it to work even once.  Any AJAX experts that can tell me how to write this basic two-part script... ?

 

I'm more of a designer than a developer, but I know some HTML, CSS, and Javascript, and PHP, but I would not describe myself as good at any of them. I know just enough to get things to work the way I want with a little help sometimes, even if it takes hours and hours of burning time.

Link to comment

these forums are not too useful eh?

I'm more of a designer than a developer, but I know some HTML, CSS, and Javascript, and PHP, but I would not describe myself as good at any of them. I know just enough to get things to work the way I want with a little help sometimes, even if it takes hours and hours of burning time.

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.