My blog has its posts links. What I need is code that will do this: when a user clicks on a post link, a parameter will be added to the link which I'll use with a get function on the other page.
This is my link - link which is visible and indexed by google:
my post link
When a user clicks on it, I need a way to add ?car=type to the link, http://www.mysite.com/post1/?car=type.
I'd like to do this with jQuery.
P.S.
I can't just add my car variable to the links in normal html, becouse Google will index them badly, the variables change every day and I'd get pages not found all over the serps.
Any idee?
Ty!
Well...
$(document).ready(function() {
$("a").click(function() {
this.href += "?car=type";
});
});
Live test case: http://jsfiddle.net/y6PrF/
If you mark the desingated links in a way such as a class (say addType):
<a href="http://www.mysite.com/post1/" class="addType" >my post link</a>
you can do something like this on document load, no need to wait for click to do it:
$(function() {
$("a.addType").attr("href",function() {return this + "?car=type";});
});
Here's a jsfiddle example: http://jsfiddle.net/nbKPu/
Related
I am writing an application that I want to make more user friendly by removing the amount of clicks needed to navigate.
At the moment pages are loaded like so:
<a class='pageloader' name='page1.html'>Page 1</a>
<script>
$('.pageloader').click(function(){
// Load the page that the user clicked on by taking the name property and appending it to includes/
$('.content').load("pages/" + this.name);
});
</script>
Basicly this takes the name of the clicked link and replaces the content div's content with whatever is inside the file that matches the name property.
Now my only problem is that I can't redirect people to pages using HTML because the only page that has proper styling is index.php.
Obviously I am now redirecting people to index.php after an action is finished, but I would like to redirect them to a specific page.
I've thought about calling
$('.content').load('pages/edit-plein.php');
(This code is inside a .php script that writes to a file)
But that only gives me an error since it cannot find the .content div.
Would anyone know a good method to redirect a user to the page I want?
As far as i understand you want to make shure the right content gets loaded (inside that div) when you share the link to a specific subpage on your site, but can only share a link of your index.php because of its styling.
I would suggest you add a variable to your URL, i.e. like
index.php?page=edit-plein
then get that var with PHP and create a JS call to your pageloader, like this:
<?php
if ( $_GET['page'] != '' ) {
echo '<script>$(".content").load("pages/'. $_GET['page'] .'");</script>';
}
?>
This is not a good way for links:
<a class='pageloader' name='page1.html'>Page 1</a>
Maybe you can try this:
Page 1
Page 2
And JS must be like this:
$('.pageloader').click(function(){
$('.content').load("pages/" + $(this).attr('id') + ".html");
});
I hope this solves your problem.
I have jQuery site here. I have 4 links with in ul tag. Within this link, I need to inactive a link but have to show. See below:
<div class="over hide" id="waterSecondOver">
Overview
Local Area
Floor Plans
<a href="javascript:void(0);" id="chancingallery" >Gallery</a>
</div>
I have a click function on the above links (except the 4th one i.e gallery). See the code
JQR('#waterSecondOver a').click(function() {
// my code
});
Here I need to show the gallery link, but nothing happens when click on the gallery (i.e must be inactive link or what I say it's functionality is only show the link as "gallery ")
I think it's clear . Please help me
JQR('#waterSecondOver a').click(function(event) {
event.preventDefault();
});
Don't set hrefs in the anchor tags to javascript:void(0); - this is messy because it's needless repetition. With jQuery, it's possible to use return false; in your event callback to avoid the default click event taking you to a new page or the current one. While this is the easiest method, it's not always the best. See this article for more info:
http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/
But you definitely don't want to be setting href manually.
I would simply change the class of the anchor(lets say class="active") and everytime onclick check for that "active" class. If the class is "active" do nothing else DO SOMETHING.
You can do this way for simplicity...
JQR('#waterSecondOver a').click(function(event) {
if(JQR(this).attr('id') == "chancingallery"){
// inactive all links except the one you dont want
}else{
// whatever you need....
}
});
i have one page on that page having multiple images.On each image have their own share and like button.when i click on share button i want to share that perticular image but that is not happening please help me.
here is my code..
<script type="text/javascript">
var str;
function fbs_click(u,val)
{
var i;
var imgobj=document.getElementById(val).getElementsByTagName('img');
for(i=0;i<imgobj.length;i++){
str=imgobj[i].src;
//document.write("<br>");
}
alert(str);
//This is to show fb popup for sharing passed pageurl
window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u),'sharer','toolbar=0,status=0,width=626,height=436');
return false;
}
</script>
<!--This is for setting img for fb like -->
<script type="text/javascript">document.write('<link rel="image_src" type="image/jpeg" href="'+str+'">');</script>
$url="http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
<div class='sharebutton'>
<a href='#' onclick='return fbs_click(\"$url\",\"$src_value\");' class='fb_share_link'>Share on Facebook</a>
</div>
i have paste the code is short just understand you my problem
The problem is, you're only sending the page url. Facebook indexes its like counts based on the url.
Since Facebook has no idea which image on the page the user is intending to share, it grabs your default meta tags for the page and that's what shows up on the user's timeline. Since the urls for each photo are the same, sharing any one photo will result in the counter being incremented for all photos.
One way to make this work would be to create a separate page for each image on your site and pass that in your onclick function. That page has specific metadata for that image the Facebook parser can use. When a user follows that link, have a script on that page to redirect them back to your main page (preferably to an anchor at the right photo). You'll need to have some way to fail gracefully for people who don't have javascript enabled.
I don't know which tag or event to use. But I would like some way to make jQuery do a click function or "click" a link when the page is loaded. The script is getting a specific data from the URL. If the url sais: www.domain.com/index.php?a=1&b=1234 then I would like something like this in the beginning:
<?php$a = $_GET['a'];$b = $_GET['b'];if ($a == '1') {//echo jQuery script that automatically clicks the <a> tag}?>
and then the HTML script is like this:
jQuery Click
Please help me
You'll need the .trigger method.
$('a#YOUR_ID').click();
or
$('a#YOUR_ID').trigger('click');
or
$('a.YOUR_CLASS').click();
or using href
$('a[href^="page2.php"]').click();
First one is preferred. You should make selector with your ID of CLASS belongs to that a.
NOTE: a[href="page2.php"] means a tag which href beginning exactly with page2.php
If none of above works then try:
window.location = document.location.hostname + '/' + $('a[href^="page2.php"]').attr('href');
Two open a popup try this:
var urlToOpen = document.location.hostname + '/' + $('a[href^="page2.php"]').attr('href');
window.open (urlToOpen,"mywindow","menubar=1,resizable=1,width=350,height=250");
Detail about window.open()
Do this :
$("a[href='page2.php']").click();
or prefferably use an ID
$("a#myID").click();
You can get more information in the jQuery documentation or perhaps a tutorial at jQuery.
To answer your specific question, I believe you are looking for something similar to:
$(document).ready(function() {
$('#autoclick').click();
});
My recommendation would be to apply an id in the corresponding html:
<a id="autoclick" href="/somewhere-else">This is a link</a>
Hope that helps you get started.
Note: The click() has this as a specific shortcut for trigger("click") since v1.0. Ref: jQuery click
So I'm experiencing problem - I got a function, when someone clicks a menu, it will show a div tag. See here -
$("a#cat").click(function() {
$("div#categoryBox").show();
return false;
});
So far everything works great, the div content shows up excellent, but the problem is that inside div content there are buttons (a tags), delete and edit, when I click one of these buttons, the div tag hides. The button links are -
<a href="?action=edit&id=<?php echo $id; ?>"> and <a href="?action=delete&id=<?php echo $id; ?>">
If I press one of these links, the div content automatically hides, and I need to press again the a button with id #cat. Is there any way to make it stay, unless I press different menu link or refresh page?
If you need any additional information, please ask.
May be the page is reloaded when you click on edit/delete links, so you think the div gets hidden. If you are doing any client side implementation on edit and delete click then you should make sure to prevent the default behavior or those links. Try this.
$('#categoryBox a').click(function(e){
e.preventDefault();
});
There are several ways to do this. Perhaps the easiest is to re-show the div when the page loads if certain conditions are met.
If you want to display the div every time the url is ?action=edit or ?action=delete, use this:
$(function () {
if (/\baction=(edit|delete)\b/.test(location.search)) {
$("div#categoryBox").show();
}
});
Or, you could append a hash parameter when you want to show the div:
edit
delete
$(function () {
if (location.hash === "#showCategoryBox") {
$("div#categoryBox").show();
}
});