Is there any way to listen for hashchanges in the url and log the event for use of a backbutton in AJAX etc.
Using links like such:
Go to page!
and script like this:
<script type="text/javascript">
$(function () {
if (window.location.hash){contentload(window.location.hash);}
$('a').click(function() {
fragment = this.hash;
contentload(fragment);
});
});
function contentload(fragment) {
fragment = fragment.slice(1).replace('!', '')
$('#content').load('http://mysite.com/'+fragment+'?ajax=1');
}
</script>
I need to try and save the state of the page, Ive seen the jQuery address plugin but have no idea how to implement this..
take a look at this jquery plugin
http://benalman.com/projects/jquery-bbq-plugin/
i solved this using the jQuery Address plugin
Related
Below is my code
<a class="foot" href="<?php echo someurl.com?id;?>" >Info</a>
Iam setting a click function for the class 'foot'
$('.foot').click(function(){
alert('run some functions');
});
As you can see on the code above First it runs Jquery later on it will be passed to specified Url... But is it possible to pass to specific url then run Jquery.. ???
You have to use Ajax request if you want to call the URL without moving to another page. otherwise your javascript code won't execute.
http://api.jquery.com/jQuery.ajax/
if you are doing this for a fallback in case your client doesn't support Javascipt then you have to do it like this.
$('a').click(function(e){
e.preventDefault();
//your code
});
if you want the code to run after page load then I suggest you introduce:
$(document).ready(handler)
That way jquery runs AFTER page load.
You need to use prventDefaults and then trigger document.location.href to your clicked link.
http://api.jquery.com/event.preventDefault/
Hopefully this will solve your problem.
Why you are not using $(document).ready(handler)
<script type="text/javascript">
$(document).ready(function() {
$('.foot').click(function(){
alert('run some functions');
});
});
</script>
<a class="foot" href="hello.php" >Info</a>
i am using drupal and ihve successfuly loaded a node creation form using jquery load function
<div id='test'></div>
<a href='#' id='test2'>test</a>
so when clicking link it will load form
var ajax_load = "loading...";
var loadUrl = "NODE_CREATE_PATH";
$("#test2").click(function(){
$("#test").html(ajax_load).load(loadUrl);
});
now my problem is all scripts like date popup is not working for this loded form . someone suggested me using http://api.jquery.com/live/ , but i dont know how to use.
please help
This is because the DOM is being reloaded with the new HTML. Therefore your bindings on these nodes will not work anymore.
You could use: http://api.jquery.com/on/ (the live event is deprecated) for other bindings but not for the datepicker for as far as I know.
You could do something like this:
function bindStuff(node) {
node.find('.datepicker').datepicker();
// Do some more bindings here
}
$("#test2").click(function(){
$("#test").html(ajax_load).load(loadUrl, function() {
bindStuff($("#test"));
});
});
$("#example2").click(function(){
$("#example").html(ajax_load).load(loadUrl, function() {
bindStuff($("#example"));
});
});
using Drupal.attachBehaviors() we can :)
example
$(id).load(url, function(){
Drupal.attachBehaviors(this);
});
On my site in the bottom left hand corner there's a chat tab that you can open and close with a click. The button is named 'trigger' and the chat panel is named 'panel'. I'm not familiar with javascript really, I just cobbled this together from existing scripts, anyway, the code I use that works this is:
<script type="text/javascript">
$(document).ready(function(){
$(".trigger").click(function(){
$(".panel").toggle("fast");
$(this).toggleClass("active");
return false;
});
});
</script>
What happens though is if the user opens chat then goes to another page, the chat must be reopened again. I need a way to keep the chat open if it's already open.
Maybe something in the body onload tag? And using sessions?
Note: My site's in php
With jquery-cookie you can save state of the chat class toggle. Something like this:
<script type="text/javascript">
$(document).ready(function(){
if($.cookie('panel-active')) {
$(".trigger").toggleClass("active",true);
}
$(".trigger").click(function(){
$(".panel").toggle("fast");
$(this).toggleClass("active");
$.cookie('panel-active', $(this).hasClass("active"), { path: '/' });
return false;
});
});
</script>
There's a great tutorial on IBM's website which walked me through a simple search/results list using jQuery,PHP and Ajax.
I was able to make it work and it's really cool.
One problem. I want the results to be hyperlinks and I can't get any java script to run on the results.
Here is the script I have (includes what was in the tutorial plus the additional script necessary to ovverride the hyperlink, click behavior):
<script type='text/javascript'>
$(document).ready(function(){
$("#search_results").slideUp();
$("#search_button").click(function(e){
e.preventDefault();
ajax_search();
});
$("#search_term").keyup(function(e){
e.preventDefault();
ajax_search();
});
$("a").click(ClickInterceptor);
});
function ajax_search(){
$("#search_results").show();
var search_val=$("#search_term").val();
$.post("./find.php", {search_term : search_val}, function(data){
if (data.length>0){
$("#search_results").html(data);
}
})
}
function ClickInterceptor(e)
{
window.alert("Hellow World!");
return false;
}
</script>
If i put the following html under the <body> tag:
this will work
That will display the alert window.
However, if I change the results to hyperlinks (found in find.php, listing 7 from the tutorial):
$string .= "".$row->name." - ";
It does not work.
Any idea on how to fix this?
The click function binds when it is run. You need to change it to a live binding.
$("a").live("click", ClickInterceptor);
Or you can just bind it when you update the search results by putting the following after $("#search_results").html(data):
$("#search_results a").click(ClickInterceptor);
The problem is pretty simple actually, $("a").click(ClickInterceptor); will only look for items that currently exist in the DOM. All you need to do is change that line to this:
$("a").live("click", ClickInterceptor);
I'd also advise taking the time to read more about jQuery's Events/live.
Hai
i want to generated an automated click event. I am working in php server, i Know Javascript.
Below is my Code
<script language="javascript">
function autoClick() {
var elm=document.getElementById('thisLink');
elm.click();
document.getElementById('thisLink').click();
}
</script>
</head>
i put this on inside the body tag :
onload="setTimeout('autoClick();',3000);"
and inside the a tag :
href="./apage.php" id="thisLink" name="thisLink" target="newWindow"
But it doesn't work in MOzilla Is any solution , 0r any other solution ???
Thanks in advance
You could try JQuery's trigger function.
$('#thisLink').trigger('click');
This should possibly work although I haven't tested it.
JQuery: http://jquery.com
doc: http://docs.jquery.com/Events/trigger#eventdata
Element.click works only on input elements on Mozilla. Try something like
function autoClick() {
var elm=document.getElementById('thisLink');
document.location.href = elm.href;
}
instead, or if you prefer opening the link into a new window,
function autoClick() {
var elm=document.getElementById('thisLink');
window.open(elm.href, 'autoclickwindow');
}