I'm trying to set the base href with JQuery by reading a PHP file.
Why does $("base").load("ip.php");
work but $("base[property='href']").load("ip.php");
doesn't?
Of course what I need is to set the href parameter only, not the content of base.
I found I could get it working by using $("head").load("ip.php");
but would like to know why above isn't working.
I'm trying to set the base href with JQuery by reading a PHP file.
Why does $("base").load("ip.php");
work but $("base[property='href']").load("ip.php");
doesn't?
Of course what I need is to set the href parameter only, not the content of base.
I found I could get it working by using $("head").load("ip.php");
but would like to know why above isn't working.
-
what do you mean with "this work"
$("base").load("ip.php");
, how do this set the href of base? or this:$("head").load("ip.php");
?? – reyaner Commented Sep 30, 2014 at 7:26 -
load
doesn't change the href attribute, it loads content from your ip.php and puts it intobase
element, if you want to change href you can do$('base').attr('href', 'ip.php')
– paulitto Commented Sep 30, 2014 at 7:33
4 Answers
Reset to default 3Try something like this to set href value
$('base').attr('href','http://www.google.');
Its depend how your file ip.php is
For example
ip.php
<?php
echo 'http://www.google.';
?>
Then
$.get('ip.php',function(response){
$("base").attr("href", response);
});
I don't know exactly, but in my opinion, u got the meaning of the selector wrong. I think [property='href'] is not a valid selector. href is the property you should search for, and not the value.
You can select DOM-elements with jquery using $("base"). Additionally you can use a key-value bination to search for certain attribute-values of your element.
$("a[href$='']")
With this selector you would select elements with a href attribute which ends with ''.
So the point is: you still select the DOM-element and NOT a part of that element. But your search is better specified.
just need :
window.location = "http://yoursite.php";
dont use load, use $.ajax!
$.ajax({
url: "ip.php"
}).done(function (data) {
$("base").attr("href", data);
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745580249a4634211.html
评论列表(0条)