Android sending sms onload JS - javascript

Hello i use this script on my landing pages, but the user needs to click a button for it to work.
<a onclick="ClickTrack('click5')" href="smsto:NR?body=TEXT" class="btn_2" >
<strong><span class="cta-clik">Click to send sms</span></strong>
</a>
Does anybody know how to make it work without button click but when website Loads?

Personally I dislike the inline bindings but you could try putting it on your body tag
<body onload="ClickTrack('click5')">
If possible bind in your logic or just call it like #randy was saying.

For the redirect part, you can make a function like this:
function redirect()
{
window.location.href = "smsto:NR?body=TEXT"
}
If you use JQuery, you could use document.ready inside your script file:
$('document').ready(funtion(){
redirect();
});
If you don't, include the script in the bottom of your page like this:
<body>
<!-- content here -->
<script>
document.addEventListener("DOMContentLoaded", function(event) {
redirect();
});
</script>
</body>

Related

Auto Click on hyperlink

I would like to a HTML code with the following functions if you guys can help , please.
Once the page loads, after 1 second to auto click on a hyperlinked text , how do I do this ? For the new website to be opened in the same window.
I have this code but this one is sending me to a new window and the pop up is blocking - not good
<!DOCTYPE html>
<html>
<body>
<head>
<script>
function autoClick(){
document.getElementById('linkToClick').click();
}
</script>
</head>
<body onload="setTimeout('autoClick();',700);">
<a id="linkToClick" href="http://www.google.com" target="_blank">GOOGLE</a>
</body>
</body>
</html>
Thanks
Instead of using <a href=> you should try just setting the variable window.location.href. So your code should look something like this:
<body onload="window.location.href='http://www.google.com',700);">
https://www.w3schools.com/howto/howto_js_redirect_webpage.asp
Let me preface this by saying this is extremely questionable when it comes to web design, and generally regarded as a shady or bad practice. That said:
$(window).on('load', function(){
window.location = 'http://example.com'
});
OR (vanilla JS)
window.onload = function(){ window.location = 'http://example.com' };
Again, be careful with this. If you want to do something like this, you need a pretty good reason why.
Possible duplicates:
Redirect from an HTML page
HTML redirect on page load

Issue When Dynamically Removing Script Tags With Jquery

I have created two short javascript files, each containing a $(document).ready function that has javascript to detect a button click from the html file that has included it. My main html file has the script tags pointing to each file in the header:
file1.js:
$(document).ready(function(){
$('.wrapper').on('click', '.click_1', function(){
alert('hello from the first file');
});
});
file2.js:
$(document).ready(function(){
$('.wrapper').on('click', '.click_2', function(){
alert('hello from the second file');
});
});
My goal, however, is to be able to dynamically remove one of the script tags (the javascript from the second file) from the header, and its functionality along with it. To do so, I created a script in my main html file to remove the target script tag via the src attribute. However, while an inspection of the page source reveals that the third script tag has indeed been removed, its functionality remains. For instance, even after clicking the .remove_2 button, I can still click the .click_2 button and receive the "hello from the second file" alert:
main.html:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="file1.js"></script>
<script type="text/javascript" src="file2.js"></script>
</head>
<body>
<div class="wrapper">
<button class='click_1'>File1</button>
<button class='click_2'>File2</button>
<button class='remove_2'>Remove File2</button>
</div>
</body>
<script>
$(document).ready(function(){
$('.wrapper').on('click', '.remove_2', function(){
$('script[src="file2.js"]').remove();
});
});
</script>
</html>
In short, I wish to be able to dynamically remove a script tag so that the javascript in the file that the tag points to no longer has any affect on the html page. However, I have not been able to accomplish this. Can anyone tell me what is wrong with my code? Also, is what I am trying to accomplish even possible? Thank you.
Removing an external script does not remove event handlers. They are attached to current document.
A solution can be:
remove the script
get all html page
replace html page with new content
$('.wrapper').on('click', '.remove_2', function(){
$('script[src="file2.js"]').remove();
var html = document.documentElement.innerHTML;
document.open('text/html');
document.write(html);
document.close();
});
In jQuery, replacing only the header after removing the script:
$('.wrapper').on('click', '.remove_2', function(){
var header = $('html head');
header.find('script[src="file2.js"]').remove();
$('html head').replaceWith(header);
});
Try unbinding the click event from the second button before removing it:
$('.click_2').unbind("click");
Although unbind is now deprecated. The newer form is 'off':
$('.click_2').off( "click", "**" );
http://api.jquery.com/off/
That said, you do seem to be using a rather peculiar approach to disable click functionality.

Make popup on load working

I'm trying to make a pop up right away when page is loading , but all the browsers are blocking it and don't let the pop up works , this is my code :
<html>
<head>
<script>
function codeAddress() {
javascript:void window.open('http://google.com','1411977082597','width=750,height=550,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');return false;
}
window.onload = codeAddress;
</script>
</head>
<body>
</body>
</html>
There is a solution for that ?
do not need following code
javascript:void
this code use when write javascript on html tags
function codeAddress() {
window.open('http://google.com','1411977082597','width=750,height=550,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');
}
Check your browser javascript enable.
Check your browser allow popup.
Allow popup to work using Javascript is impossible.
You can do it by calling your method in jquery document.ready method. like:
$(document).ready(function(){
codeAddress();
})

Function redirect if Div Id on my page won't work

I need to redirect page if div id found inside the page.
I have this script :
<script>
if (!document.getElementById("locationsHolder")) {
window.location.href = "logged.html";
}
</script>
On the same page I have this
<div class="locationsHolder" id="locationsHolder"></div>
Although I have everything right it loads logged.html page whatever id I put on getElementById. i.e. getElementById("notexist")
You should do if (document.getElementById("locationsHolder")) since you want to do something if the div is found. Also you should put the script at the end of document or use jQuery $(document).ready() to make sure the entire page is loaded prior to running the script.
Maybe this will do what you need:
<!-- Put the following somewhere in the head block: -->
<script type="text/javascript">
window.onload = function(){
if(document.getElementById("locationsHolder") == null){
// Do something here if the ID is NOT found on the page
}else{
// Do something here if the ID IS found on the page
document.location = "logged.html";
}
};
</script>
you probably tested for the existence of div id before even the page has been loaded.
I would suggest you to either place the script after the div id or invoke the call only after the onload event.
window.onload=function(){
// your code
}
you should ask if it is == null. try this in the developer tool.
!null --> true
!"" --> true
!document.getElementById("locationsHolder") is always true.
you should do something like
!document.getElementById("locationsHolder") == null
It was the '!' that I remove it and script was before div id. Now it's working.
I need it this because I want home page redirected if another page contain a div id. I have another script inside index file that looks for page2 if contain div id , and it worked until I made this script from this topic that checks if that div is present on the page.
So, this script is one home page and checks if div id found on page2 and loads it on home page :
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function($){
$('#result').load('Page2.html #intro');
});
</script>
</head>
<body>
<div id="result"></div>
Code from page2:
<div id="result"><div id="intro">Hi, this is the Intro!</div></div>
Now the script from this topic can't find div id "intro" , and actually it dosen't load as bellow script search for 'intro'(and jquery above stops working), but if I search for other div from the page is working Must be a conflict.
<script>
if (document.getElementById("intro")) {
window.location.href = "logged.html";
}
</script>
What is wrong?
Maybe there is another more simple way to trigger redirection if page.2 contain div id "intro" ?
I tried to trigger redirect directly from page1 with this script but won't work:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"> </script>
<script type="text/javascript">
if ('page2.html #intro').length === 0){
window.location.href = "logged.html";
}
</script>

simple javascript not working

I've been having trouble with javascript. I don't think its my code but possible some outside factor. I've tried the following code on chrome and firefox and I can't get the alert to pop up. Nothing happens when I click the link. The code below is obviously not on my site, but I'm just using it as an example as to why other parts of my javascript aren't working.
<html>
<head>
<script language="javascript">
function art() {
alert("jdsklfs");
}
</script>
</head>
<body>
<a href='#'>click</a>
</body></html>
Well, try calling it ;~)
function art() {
alert("jdsklfs");
}
window.onload = art; //<= now the function will execute on page load
or provide the href with an id (<a href='#' id='artclick'>click</a>) and assign a click handler to it on load
function art() {
alert("jdsklfs");
}
window.onload = function(){
document.getElementById('artclick').onclick = art;
}
You are not calling the art function.
Simplest, but dirtiest is to have:
click
try adding
onclick="art();"
to your anchor tag
Change the
<a href='#'>click</a>
to
click
art() isn't tied to the link in any discernible way.
Change your a tag to:
click

Categories

Resources