Here is an api that gives ip address.
https://jsonip.com/
I want ip address in variable for my application. I can get it like that
window.onload = function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://jsonip.com/?callback=DisplayIP";
document.getElementsByTagName("head")[0].appendChild(script);
};
function DisplayIP(response) {
document.getElementById("ipaddress").innerHTML = "Your IP Address is " +
response.ip;
alert(response.ip); // alerts ip address
}
I have to use it with Sharpspring forms so the above method can't be integrated with sharpspring code. I want something like that
var ip = 'ip address';
For your easy understanding, here is the sharpspring form code
<script type="text/javascript">
var ss_form = {'account': 'MzawMDEzNjI0BwA', 'formID': 'SzQ1MTAzSEzSNTG2NNQ1STJN1k1KMjfVNTIzSwbCJKMUS2MA'};
ss_form.width = '100%';
ss_form.height = '1000';
ss_form.domain = 'app-3QNBWW1ZDA.marketingautomation.services';
ss_form.hidden = {'field_3270188034': 'ip address'}; //here I want to use that ip
</script>
<script type="text/javascript" src="https://koi-3QNBWW1ZDA.marketingautomation.services/client/form.js?ver=1.1.1"></script>
Because the script require the use of document.write, I don't think it's possible in a single document. Rather, you can fetch the IP on the initial page load, then load the page again and synchronously create the ss_form object, and inject the form.js:
if (!sessionStorage.ip) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://api.ipify.org?format=jsonp&callback=DisplayIP";
document.head.appendChild(script);
window.DisplayIP = function DisplayIP(response) {
sessionStorage.ip = response.ip;
window.location.href = window.location.href;
}
} else {
const ip = sessionStorage.ip;
sessionStorage.removeItem('ip');
window.ss_form = {
account: 'MzawMDEzNjI0BwA',
formID: 'SzQ1MTAzSEzSNTG2NNQ1STJN1k1KMjfVNTIzSwbCJKMUS2MA',
width: '100%',
height: '1000',
domain: 'app-3QNBWW1ZDA.marketingautomation.services',
hidden: {
'field_3270188034': ip
}
};
const script = document.createElement('script');
script.src = 'https://koi-3QNBWW1ZDA.marketingautomation.services/client/form.js?ver=1.1.1';
document.head.appendChild(script);
}
(remove the current form.js from your HTML)
Related
I have had the website for 3 years. This website is created by HTML, CSS, and JavaScript. I have made it by myself and there is always a WhatsApp icon at the bottom of the right. But 6 months ago, it disappeared. I looked up my code and there are no changes. How can I make my WhatsApp ıcon visible again?
Here is my code:
<script type="text/javascript">
(function () {
var options = {
whatsapp: "+9000000000", // Contact Number
call_to_action: "Merhaba, Size nasıl yardımcı olabilirim?",
position: "right",
};
var proto = document.location.protocol, host = "whatshelp.io", url = proto + "//static." + host;
var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = url + '/widget-send-button/js/init.js';
s.onload = function () { WhWidgetSendButton.init(host, proto, options); };
var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x);
})();
//</script>
The domain whatshelp.io is currently timing out, which is probably why the buttons are not working.
Based on archive.org, it appears at some point the site started to redirect to bothelp.io. However, updating the script to https://static.bothelp.io/widget-send-button/js/init.js returns a 404.
After some googling, I landed on this page: https://apps.shopify.com/whatshelp-chat-button
The description noted the following:
The GetButton widget (former WhatsHelp widget) takes website visitor directly...
Again, updated the script to https://static.getbutton.io/widget/bundle.js and success!
I then updated the configuration script you provided to the following (formatted for legibility):
<script type="text/javascript">
(function () {
var options = {
whatsapp: "+9000000000", // Contact Number
call_to_action: "Merhaba, Size nasıl yardımcı olabilirim?",
position: "right",
};
var proto = document.location.protocol,
host = "getbutton.io",
url = proto + "//static." + host;
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = url + '/widget-send-button/js/init.js';
s.onload = function () { WhWidgetSendButton.init(host, proto, options); };
var x = document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
})();
</script>
And saw the following:
Here is the script i got:
loadScriptFromUrl = function(url) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.async = false;
script._url = url;
document.body.appendChild(script);
};
Is there a way to make it wait until the script is loaded? And\or retry to load it after a while if url returns an error? (Sometimes the remote heroku server goes idle so it returns an error and I have to launch it again)
If you want to check If script is loaded or not, you can do it by adding script.onload=function(){...} .
must note '- the script.onload must come before script.src
Your code, with onload function
loadScriptFromUrl = function(url) {
var script = document.createElement('script');
script.type = 'text/javascript';
document.body.appendChild(script);
script.onload=()=>{
console.log("script loaded")
//whatever you want to do
}
script.async = false;
script.src = url;
script._url = url;
};
If you want to check for any error on loading you can go with script.onerror=function(){...}
It will also come before script.src
loadScriptFromUrl = function(url) {
var script = document.createElement('script');
script.type = 'text/javascript';
document.body.appendChild(script);
script.onerror=()=>{
console.log("script not loaded")
//reload script like below
loadScriptFromUrl(url);
//or reload the whole window by location.reload() method
}
script.async = false;
script.src = url;
script._url = url;
};
If you want to wait until the function loads the script you can put this function under an async function, add await keyword before it and return a promise resolve under script.onload();
E.g.
async function parentFunct(){
loadScriptFromUrl =await function(url) {
return new Promise((resolve,reject)=>{
var script = document.createElement('script');
script.type = 'text/javascript';
document.body.appendChild(script);
script.onload=()=>{
console.log("script loaded")
resolve();
}
script.async = false;
script.src = url;
script._url = url;
})
};
}
The above code will stop everything inside parentFunc() until the script is loaded
Read more about js promises
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
So I have this code from another article
// Load the script
var script = document.createElement("SCRIPT");
script.src = 'https://cdn.jsdelivr.net/npm/darkmode-js#1.5.5/lib/darkmode-js.min.js';
script.type = 'text/javascript';
script.onload = function() {
var $ = window.jQuery;
// Use $ here...
};
document.getElementsByTagName("head")[0].appendChild(script);
})();
Where do I add the code below to the above code
new Darkmode({ label: '🌓' }).showWidget();
I am trying to create a new window dynamically using window.open() and $(document).ready( function() { ... }); I got it working on Chrome and Internet Explorer but firefox is not trigering the jQuery code. See code below:
index.html code:
<html>
<head>
</head>
<body>
Link
<script type="text/javascript">
function showDetails(name, timeStamp)
{
var w = window.open("", timeStamp);
var s = w.document.createElement("script");
s.type = "text/javascript";
s.src = "var eventArray = [];";
w.document.body.appendChild(s);
var s = w.document.createElement("script");
s.type = "text/javascript";
s.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js";
w.document.body.appendChild(s);
var s = w.document.createElement("script");
s.type = "text/javascript";
s.src = "test.js";
w.document.body.appendChild(s);
w.document.title = name;
w.document.close();
return false;
}
</script>
</body>
test.js code:
alert("executing js file");
$(document).ready( function()
{
alert("document ready fired");
});
The first alert "executing js file" is executing but not the second one "document ready fired".
Any idea how can I make this work on firefox?
NOTE:
w.document.close(); and return false; is added following this post but still not working...
jquery is loading properly
thank you very much in advance
My inner sense says:
s.src = "jquery1.10.2.js";
should be:
s.src = "jquery-1.10.2.js";
Or any valid url that points to jquery.
Why you always use same variable names? Could you please try it like:
<script type="text/javascript">
function showDetails(name, timeStamp) {
var w = window.open("", timeStamp);
var s1 = w.document.createElement("script");
s1.type = "text/javascript";
s1.src = "var eventArray = [];";
w.document.body.appendChild(s1);
var s2 = w.document.createElement("script");
s2.type = "text/javascript";
s2.src = "jquery1.10.2.js";
w.document.body.appendChild(s2);
var s3 = w.document.createElement("script");
s3.type = "text/javascript";
s3.src = "test.js";
w.document.body.appendChild(s3);
w.document.title = name;
w.document.close();
return false;
}
</script>
On thirdpartydomain.com I want to embed a simple <script> tag that pulls in a script from mydomain.com/myscript.js, which simply creates a little <div> and pulls partial page content from mydomain.com/mypage.htm.
Here's the script, adapted from: How to embed Javascript widget that depends on jQuery into an unknown environment
var myEmbedId = '12345';
var myEmbedContainerId = 'myEmbedContainer_' + myEmbedId;
document.write('<div id="' + myEmbedContainerId + '">IF ALL GOES WELL, THIS TEXT WILL BE REPLACED WITH MYPAGE.HTM CONTENTS');
document.write('</div>');
(function (window, document, version, callback) {
var j, d;
var loaded = false;
if (!(j = window.jQuery) || version > j.fn.jquery || callback(j, loaded)) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://mydomain.com/jquery-1.4.1.min.js";
script.onload = script.onreadystatechange = function () {
if (!loaded && (!(d = this.readyState) || d == "loaded" || d == "complete")) {
callback((j = window.jQuery).noConflict(1), loaded = true);
j(script).remove();
}
};
document.documentElement.childNodes[0].appendChild(script)
}
})(window, document, "1.3", function ($, jquery_loaded) {
$(document).ready(function () {
alert('jquery loaded!');
var myRefreshUrl = 'http://mydomain.com/mypage.htm';
alert('refreshing from ' + myRefreshUrl);
$.get(myRefreshUrl, function(data){
var returnData = data;
alert('return data: ' + data);
$('#' + myEmbedContainerId).html(data); });
alert('load complete v2');
});
});
In IE, I get an Access Denied error from Javascript; in Firefox I just get no data returned.
What's wrong with this?
You cannot create an AJAX request to a different domain from the one that is hosting the current window context.
To pull off what you're describing, you can do something like:
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://mydomain.com/dynamic.js?data=somepage.htm';
someContainer.appendChild(script);
Within that dynamic.js, you can wrap the HTML contents in a document.write(). The net effect is the same as inserting the result of the AJAX request at the same point in the DOM.