Reputation: 3257
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.
Upvotes: 1
Views: 6238
Reputation: 10896
Try something like this to set href value
$('base').attr('href','http://www.google.com');
Its depend how your file ip.php is
For example
ip.php
<?php
echo 'http://www.google.com';
?>
Then
$.get('ip.php',function(response){
$("base").attr("href", response);
});
Upvotes: 3
Reputation: 2819
dont use load, use $.ajax!
$.ajax({
url: "ip.php"
}).done(function (data) {
$("base").attr("href", data);
});
Upvotes: 0
Reputation: 395
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 combination to search for certain attribute-values of your element.
$("a[href$='.org']")
With this selector you would select elements with a href attribute which ends with '.org'.
So the point is: you still select the DOM-element and NOT a part of that element. But your search is better specified.
Upvotes: 1