javascript:
location.href("somefile.php"); // successfully working with IE
location.href = "somefile.php";
Ques 1. first code is not work with Safari. Why?
Ques 2. What is the difference b/w these codes.
Thanks,
href is not a method, it is a property whose value is a string.
The first is a call on a method with a url as the argument (incorrect), the second is assigning the url as the value of the property (correct).
See also: http://www.w3.org/TR/Window/#location
i never heard of location.href("somefile.php");... location.href = "somefile.php"; is the "normal" way that you should use.
Also it's more efficient using window.location than location. So try to use :
window.location.href = "somefile.php";
(as Andy said, href is a property and in JS you specify a property value in this way: object.property = "value")
Answer 1 :- It wont work because href is a property of location object, not a method.
Answer 2 :- location.href("...") denotes a method(that is invalid) while location.href is a property.
Related
I need to get current URL (in javascript window.location.href) for value of my <spring:param> (in context of creating back URL). For param's value I have to use expression language. Is that even possible, or What are you suggesting? Thank you for answers!
I need to get current URL (in javascript window.location.href)
It's available by the HttpServletRequest#getRequestURL().
So, in EL thus just ${pageContext.request.requestURL}.
It's available by the Read This.
So, in EL thus just ${pageContext.request.requestURL}.
I'm not exactly sure how to handle multiple instances like this. I know in normal JS I can simply use [0] and such.
My code is this:
location.href = $('a.test').attr('href');
I need to handle both the first instance of test and the second. Is there a simple
location.href = $('a.test')[0].attr('href');
I'm missing or such?
$('a.test')[0] return a dom element reference which does not have the method attr(), so your script will fail
use .eq(index)
location.href = $('a.test').eq(0).attr('href');
or
location.href = $('a.test:eq(0)').attr('href');
You are trying to call attr on javascript DOM object instead of using jQuery object as indexer returns the DOM object Use eq() to get jQuery object
location.href = $('a.test').eq(0).attr('href');
You can use DOM object with href instead of attr
location.href = $('a.test')[0].href;
location.href = $('a.test').eq(0).attr('href');
or you can try
location.href = $('a.test:eq(0)').attr('href');
reference eq() and :eq() and attr
This demo might help: working demo http://jsfiddle.net/CmQGu/
you can also use nth-child api demo here: http://jsfiddle.net/wYdyJ/
There are many ways you can approach this, like I showed you in demos. also if you keen read this : Difference between .eq() and .get() and :nth-child()?
API:
first : http://api.jquery.com/first/ (All the peeps up have missed this)
eq : http://api.jquery.com/eq/
nth-child : http://api.jquery.com/nth-child-selector/
Code:
alert(location.href = $('a.test').eq(0).attr('href'));
alert(location.href = $('a.test').first().attr('href'));
alert(location.href = $('a.test:nth-child(2)').attr('href'));
Hope it helps :)
document.getElementById("myFrame").setAttribute("src") = "http://www.yahoo.com/";
myFrame is an iframe element... when entering this into the chrome developer console, it gives me the error "Invalid left-hand side in assignment" I am trying to update the iframe. Is there a method I am forgetting?
setAttribute takes two arguments:
document.getElementById("myFrame").setAttribute("src", "http://www.yahoo.com/");
You are trying to set the DOM object to the string "http://www.yahoo.com/" ... which is invalid.
You don't really need setAttribute when setting the src property:
document.getElementById('myFrame').src = 'http://www.yahoo.com/';
You can't assign an function call to something, try this instead:
document.getElementById("myFrame").setAttribute("src", http://www.yahoo.com/");
Here, I fixed it
document.getElementById("myFrame").setAttribute("src", "http://www.yahoo.com/");
Try this...
document.getElementById("myFrame").setAttribute("src","http://www.yahoo.com/");
Exists any JavaScript or Objective-C method to convert a location.href="MyURL" to ??
I have over 200 location.href URL not working with UIWebViewNavigationTypeLinkClicked :-S
Thanks to everyone can help me!
<script type="text/javascript">
document.getElementById("myAnchor").setAttribute("href", window.location.href);
var loc = document.getElementById("myAnchor").getAttribute("href");
var t = document.createTextNode(loc);
document.getElementById("myAnchor").appendChild(t);
</script>
Here is one way of doing it: document.getElementById() grabs a reference to the ID attribute of your anchor(s). The second argument of setAttribute(), "window.location.href" grabs a reference to the url in the current window and sets the href attibute in your anchor to this, however if you have a bunch of location.href()s declared in your code, then store these as a variable before the first line instead and then reference that variable at around the same line as where I have declared "loc". The variable "loc" declares a variable which stores a reference to the newly created href attribute you just declared in the previous line. Then I am declaring a variable "t" which creates a text node in the DOM, with the href from the previous line as the value this variable will hold. Lastly, I use document.getElementById to get "myAnchor" again and append the text node from the previous line to it. So now we can actually see the url in the link.
//Also, use a for loop to run this action 200 times and correct all of the hrefs on your page.
<script type="text/javascript">
for (var i=0;i<200;i++){
//run document.getElementById() and .setAttribute, .createTextNode, etc. here
}
</script>
Working fiddle: http://jsfiddle.net/1so33q4z/36/
I would not recommend using document.write(); as mentioned in the other person's post, as this causes the page to work in a way the DOM is not meant to (writes serialized text). Especially if you need to correct 200 of them. See this post Why is document.write considered a "bad practice"?
i hae a link like ( domain.com/jsapi?key=123456 )
hov can i get this "key" into my JS code? i use jQuery and i don't know about its are easy way to get this "key" into JS variable.
tanks for all.
This plugin might helps: jquery url parser
key = $.url.setUrl($(yourlink).attr('href')).param('key');
(not tested)
It's not jquery. It's pure javascript. You can use regexp.
str = "domain.com/jsapi?key=123456" # Take it from wherever you want
splitted = str.split(/\?key=([0-9]+)/)
Then you'll have an array in the "splitted" variable, it's second element (at the id 1) containing the value.
jQuery not needed. The query string is available from the DOM:
window.location.search.match(/key=([^&]*)/);
Which gives you an array that has your value in it.
You can use the URL constructor as follows:
let url = new URL('https://example.com/jsapi?key=123456');
console.log(url.searchParams.get('key')); // Outputs 123456
Using this method you can parse and get any part of a URL.
Important:
Note that I've added the protocol (https://) to the sample URL so I make sure it is a valid URL and it can be parsed.
Take into account the browser compatibility. You can check it here
For more details you can also the the specification.