July 12th, 2018, 01:30 AM
-
July 12th, 2018, 04:23 AM
-
Your AJAX returned some nice HTML. But that's all it did. Return it.
Don't return Javascript to execute. Return a JSON response. That response, probably an object, can then contain particular data that the calling code can recognize as containing an instruction to redirect to wherever.
Last edited by requinix; July 12th, 2018 at 04:26 AM.
July 12th, 2018, 08:21 PM
-
Originally Posted by requinix
Your AJAX returned some nice HTML. But that's all it did. Return it.
Don't return Javascript to execute. Return a JSON response. That response, probably an object, can then contain particular data that the calling code can recognize as containing an instruction to redirect to wherever.
Hello;
I can't explain why it wasn't working before and now it is!
What you sau I am sure is right (most of the times) but can you explain what happened now! I changed the js from
Code:
(function(window)
{
var currentScript = document.currentScript;
var apiUrl = currentScript.src;
// this is the url of the timer
var url = createUrl(currentScript, apiUrl);
// get the timer
getTimer(currentScript, url);
}
(this));
function createUrl(currentScript, apiUrl)
{
// create URL parameters object
const paramsObj =
{
action: 'load-template',
launch_owner_email_hashed: currentScript.getAttribute('data-launch_owner_email_hashed'),
launch_id: currentScript.getAttribute('data-launch_id')
}
// convert object to an array
var paramsArr = Object.keys(paramsObj);
// add '&' character between each array item
var params = paramsArr.map(function(i)
{
return encodeURIComponent(i) + '=' + encodeURIComponent(paramsObj[i])
})
.join('&');
// add teh url path and query parameters together
var url = apiUrl + '?' + params;
return url;
}
function getTimer(currentScript, url) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
let alert = "Success!"
var response = request.responseText;
// add the timer after the current scripting code which called it
currentScript.insertAdjacentHTML('afterend', response);
// remove the scripting code so only the timer remains
currentScript.remove();
} else {
let alert = "We reached our target server, but it returned an error.";
}
};
request.onerror = function() {
let alert = "There was a connection error of some sort.";
};
request.send();
}
to
Code:
(function(window)
{
var currentScript = document.currentScript;
var apiUrl = currentScript.src;
if (!('jQuery' in window))
{
loadJQuery(initialize);
}
else
{
initialize();
}
function loadJQuery(cb)
{
var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-3.3.1.min.js';
script.type = 'text/javascript';
script.addEventListener('load', cb);
document.getElementsByTagName('head')[0].appendChild(script);
}
function initialize()
{
//Validate
var $currentScript = $(currentScript);
var params = $.param({
action: 'load-template'
, launch_owner_email_hashed: $currentScript.data('launch_owner_email_hashed')
, launch_id: $currentScript.data('launch_id')
});
$.get(apiUrl, params).then(function(html){
var div = $('<div style="margin:0; padding: 0;">').html(html);
$currentScript.after(div);
});
$(document).ready(function()
{
console.log(1);
});
}
}
(this));
Last edited by English Breakfast Tea; July 12th, 2018 at 08:29 PM.