Howto create HTML link that doesnt follow the link? - html

You know those webcams you can control over the internet? When you push the button to go left, it moves to the left.. but nothing else happens on the page.. Thats what I need to create.
I have a page that allows me to control lights in my house. When I click the button, I now have it load the php script (that controls the light) in a separate frame.. but I want to get rid of this. So basically I want to create a link that will call the php in the background, but that link won't do anything to the page its on.
Any ideas?

Use a return false; in the click event:
Not Follow the Link
Explanation
The return value of an event handler determines whether or not the default browser behaviour should take place as well. In the case of clicking on links, this would be following the link, but the difference is most noticeable in form submit handlers, where you can cancel a form submission if the user has made a mistake entering the information.
The modern way of achieving this effect is to call event.preventDefault(), and this is specified in the DOM 2 Events specification.

You will need to use ajax to achieve such a behavior.
Links that don't do anything are basically HTML links where you bind the onclick event to a JavaScript function which returns false. This makes the links "do nothing" but still executes the JavaScript which tells the camera to go left/right.

HTML 5 let's you officially use anchor elements without a href attribute. But I would just bind a Javascript event listener to whatever element your already have. I'd even add these kind of interactive elements themselves to the DOM with Javascript, since they don't serve any purpose if a user has JS disabled.

...
will give you text that looks like a link.
If it's not really a link you may wish to consider a different kind of styling to emphasize the point and so that other underlined links show as links and this shows as something else. All depends on your needs and the situation.

I like jquery...
You will notice that the onclick function returns false. This is to stop the link from working...
<a onclick="do_it(this)" ...
then in your js
function do_it(anchor)
{
jQuery.ajax(
{
url : anchor.get_attribute('href'),
data : {whatever},
type : 'POST',
success : function(data)
{
alert('woo');
}
}
)
return false;
}
Pretty much what I'm doing here is:
So when the anchor is clicked jquery POSTs to the anchor's url. You can include data if you need to. This happens asynchronously so nothing happens on your page until jQuery gets response html(or whatever). If you want to do anything with the response you can get hold of it in the success function.
When the function returns it returns false, thus preventing the anchor from doing it's usual thing.

you talking about the javascript, create a onlick event / function and implement AJAX in specific DIV area
please check this out:
http://www.w3schools.com/ajax/ajax_examples.asp
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
//You need `ajax_info.txt` file with some content
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>

You can use the following jquery solution:
HTML:
Move lights to left
JQUERY:
<script type="text/javascript">
$(document).ready(function() {
$('#link1').click(function(e) {
e.preventDefault();
$.ajax( $(this).attr('href') );
});
});
</script>

Can't believe no one has posted this yet. just use javascript void:
some click function
Its one of the oldest tricks in the book!

You need Ajax to retrieve datas from PHP without loading another page.
To "disable" the link:
Link
Or:
Link
Or just write a normal link and use jQuery (or another library) to add the event:
$('a').click(function(event) {
// the code with ajax
event.preventDefault();
});

Related

How do I make an iframe disapear after it got clicked once?

As the title says,I haven't realy started creating the code because I need a little help in here.Im not good at javascript or jquery scripting,I just started learning about html so I only know the basics.Now,getting back on topic.
I want an iframe disapear as soon as it's clicked but as I said I just started scripting.Anyone has any idea ?
Here's how you can do this with plain old JavaScript. Note that clicking the page loaded inside the iframe may not call you event handler which is why I've added a border to this example (clicking the border will execute the event handler). You may need to overlay the iframe with another element and capture the click event on the overlaid element.
<iframe src="http://someurl" onclick="this.style.display = 'none'" style='border: solid 10px red'></iframe>
you can use CSS to do this, give your iframe an id for example call it "iframe_id" like this:- #iframe_id.click{ display:none;}
Edit: as per your comment.
To include jQuery, put the following in your HTML <head></head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
Then use this w3schools article to learn how to attach javascript to HTML.
In your Javascript, you can use jQuery like this:
// Run all of the following code when our document is loaded
$( document ).ready(function() {
// Setup an event handler. Says, when we click on an iframe, run this function
$("iframe").on("click",function(){
$(this).remove();//jQuery function to completely remove from DOM
/* OR */
$(this).css("display","none"); //jQuery function that completely hides in CSS
};
});
Since you said you're new to programming HTML, you will want to read and practice JS. Here's an introduction to JS and jQuery.

jquery (mobile) - bind a tap event to a div

If I have this html:
<div id="myDiv"></div>
and this CSS:
#myDiv{
background:url('../images/someImage.png') no-repeat;
background-size:100%;
width:44px;
height:44px;
}
I need to open a new page when the user taps on myDiv. I have an external js file where I have this:
function bindMyDiv(){
$("#myDiv").bind('tap',function(event, ui){
alert("binding");
})
}
But I don't understand where to call this from the HTML, or if this is even the right way to go about this. Advice?
Try
$("#myDiv").live("tap", function(event){
alert('binding');
});
You can place this in side your onReady javascript file
EDIT:
http://jsfiddle.net/R9e6u/
Everyone here provided pretty good insight on different solutions for you to handle your script, but I don't think that anyone stopped to think SHOULD they help improve your script. "or if this is even the right way to go about this ", the answer is no. And perhaps I'm over-simplifying, but with JQM if you're trying to have a div (or any DOM element for that matter) open a new page simply wrap an anchor tag around around it (or in it, whichever is appropriate) and set your href to href="#myNewPage"and the id on the JQM page that you want to load to id="myNewPage"
jQuery Mobile's frame work is set up to automatically inject JS & AJAX into normal HTML elements to provide a smooth UX. While binding a touch event is sometime needed, this situation doesn't warrant that level of code...thats the beauty of jQuery Mobile =).
Examples of when to bind a touch event: show/hide a dom object, trigger a click for a plug-in etc.
You want to call that function on the pageinit event for the page on which it resides. You could use some other page-events from jQuery Mobile like: pagecreate, pageshow, etc. but I think pageinit is your best-bet.
The implementation would look something like this:
$(document).delegate('#page-id', 'pageinit', function () {
$("#myDiv").bind('tap',function(event, ui){
alert("binding");
})
});
OR
$(document).delegate('#page-id', 'pageinit', bindMyDiv);
You would replace #page-id with the ID of the data-role="page" element in which your div resides.
This method is preferred over event delegation for the #myDiv element because binding directly to an element creates less overhead when the event is triggered. If you use event delegation then the event has to bubble-up to the delegation root.

Overridding <a> tag click event to load data in its own <div>

I have an MVC grid which is rendered in by a json call on page load.
On clicking any tag of Grid I need to refresh this grid.
So I wrote this javascript.
$("#SearchGrid a").live("click", function (event) {
var link = event.currentTarget.attributes[0].childNodes[0].wholeText;
$("#SearchGrid").load(link);
return (false);
});
Its working fine with IE9 and other browsers. But I need to make it workable on IE8.
In IE8 its not loading the grid in same div, instead it redirects it to a new page, containing just the grid which is return from json call.
Try this. It uses the attribute href directly instead of your IE proprietary code
$("#SearchGrid a").live("click", function () {
var link = $(this).attr('href');
$('#SearchGrid').load(link);
return false;
}
not sure what the problem might be, but here are some tips on debugging it:
- var link = 'some-page.html'
try to see if you code actually returns any html data in IE8 - if it does then the problem lies in var link = event.currentTarget.attributes[0].childNodes[0].wholeText;
- instead of load(), try to use an $.ajax call or a json call and see waht happens.
I hope these will prove useful

hard link a Rel in the URL

Is it possible to make a link such that it brings up the REL of a topic,
for example if a link is
Click here
Is it possible to have a user link so when they go to the page it automatically opens the lightbox?
For example http://example.com/?rel=lightbox&src=picture.jpg
or something like that?
Yes, but no browsers include a lightbox, so you'd need to write or find code to make one of those as well.
Yes, it is possible. You may better use URLs like http://domain.com/#lightbox, and write a little script:
//jQuery sample
$(document).ready(function() {
if (location.hash == "#lightbox") {
//Just show your lightbox manually
}
});
you can do that with a bit of jQuery
$(a[rel=lightbox]).click(function(e) {
// open the lightbox with the link url inside
return false
});

HTML valid way of tracking outbound links via Google Analytics?

I am using this, and I have it tracking an outbound link, but I want my page to be valid.
This is the error I am getting: There is no "OnCick" attribute
The HTML is:
<p>View some of our student produced videos
on YouTube!</p>
I added this to my javascript embed file, to track all outgoing links:
$(document).ready(function() {
$('a[href^=http]:not("[href*=://' + document.domain + ']")').click(function() {
pageTracker._trackPageview('/out/'+$(this).attr("href"));
});
});
So now my js embed file looks like this below. I notice I call $(document).ready(function() { at the beginning of both snippets, should they both go into one?
$(document).ready(function() {
// opens links into separate window
$('A[rel="external"]').click( function() {
window.open( $(this).attr('href') );
return false;
});
});
$(document).ready(function() {
$('a[href^=http]:not("[href*=://' + document.domain + ']")').click(function() {
pageTracker._trackPageview('/out/'+$(this).attr("href"));
});
});
Use an event listener to handle the onclick.
Unless you are using some highly obscure or ancient version of HTML, you are probably using XHTML, which is case sensitive and all lower-case. Change it to onclick.
As gms8994 implied, attach click event handlers to the links you want to track, on the load event of window.
If you don't want to do that, change the page's document type declaration to point to the Transitional XHTML DTD and use "onclick" (all lower case).