user1051523
user1051523

Reputation:

change href with jquery

I need to change href tag with jquery, now i've got this.

$(document).ready(function () {
    var hScreen = $(window).height();
    var lScreen = $(window).width();

    if(hScreen < 800){

    }
});

now i want to see if hScreen is less than 800 my href will be:

<a href="javascript:" onClick="window.open('v2/main.html','longu','width='+screen.availWidth,'height='+screen.availHeight,scrollbars=1); return false;" >

else if hScreen is more i must see this:

<a href="main.htm">

thx for your help!!

Upvotes: 1

Views: 1263

Answers (6)

Pawan
Pawan

Reputation: 605

The simplest way i can think of this

$("selector for your anchor tag").attr('href',value);

Decide your value based on the condition

Upvotes: 2

Gowri
Gowri

Reputation: 16835

use attr

if(hScreen < 800){
   $('a').attr('href','main.htm');
}

Upvotes: 6

Abdul Munim
Abdul Munim

Reputation: 19217

Try this:

<a href="main.htm" id="mainLink">Goto to main Link</a>

Here id="mainLink" is required to select this element from JavaScript

JavaScript code

$(document).ready(function () {
    var hScreen = $(window).height();
    var lScreen = $(window).width();

    if(hScreen < 800) {
       $("#mainLink").bind('click', function() {
         window.open('v2/main.html', 'longu', 'width=' + screen.availWidth, 'height=' + screen.availHeight, scrollbars=1);
         return false;
       });
    }
});

This should be it, if you have hScreen < 800, you bind the click event to open up a pop up window otherwise just use href

Upvotes: 0

Pastor Bones
Pastor Bones

Reputation: 7351

Perhaps this will work for you

$(function(){
  var hScreen = $(window).height();
  var lScreen = $(window).width();

  function checkHeight(){
    if(hScreen < 800){
      window.open('v2/main.html','longu','width='+screen.availWidth,'height='+screen.availHeight,scrollbars=1);
    } else {
      window.open('main.html');
    }
  }
})

<a href="checkHeight();">

Upvotes: 2

Kemal Can Kara
Kemal Can Kara

Reputation: 416

give your link an id (for example id="aDeneme") then;

$(document).ready(function () {
    var hScreen = $(window).height();
    var lScreen = $(window).width();
 if(hScreen < 800){
$("aDeneme").attr("href","javascript:void(0)");
$("aDeneme").attr("onclick","window.open('v2/main.html','longu','width='+screen.availWidth,'height='+screen.availHeight,scrollbars=1)");
    }
else{
$("aDeneme").attr("href","main.htm");
}

Upvotes: 2

Teun Pronk
Teun Pronk

Reputation: 1399

give it a unique id and add/remove attributes.

<a id="myLink" href="javascript:"></a>

in jquery you will do

$('#myLink').attr('href', 'main.htm');

Upvotes: 1

Related Questions