Simple javascript variables question - javascript

Can anybody tell me why this line isn't working?
window.open('entertainers/drilldown.php?state=' + varlocation + '?p=','performers_frame')
I know it's simple, and I know iframes suck but i am not familiar with javascript variables.
Thanks in advance!
Ryan

should this drilldown.php?state=' + varlocation + '?p=','performers_frame')
be this
drilldown.php?state=' + varlocation + '&p=','performers_frame')
replacing the second ? with &

There's nothing jQuery about that variable. If that's a jQuery wrapped set you'd need to show us the rest of the code or tell us what it represents so you can get the attribute you're looking for. You're not providing enough for us to help you.
EDIT:
Ryan, if the variable is defined as $.varlocation then you have to append it as that. Try this:
window.open('entertainers/drilldown.php?state=' + $.varlocation + '&p=','performers_frame')
Just because it is declared with a $ doesn't mean it is a jQuery wrapped set. In this case, it isn't.

Related

Changing margin of an element with Jquery

So, I have a div, named $slideshowContainer in jquery. I want to change it's left margin on resize to be a half of its width. I tried using
$slideshowContainer.css('margin-left',' + (' + $slideshowContainer.width() / 2) + 'px)';
and other similar variations, but it seems to not work (other resizes in the same function work well, so I guess there's something wrong in code above. I guess these pluses and apostrophes have something to do with it, I never really understood when to insert them in Javascript (I usually felt when I have to insert them, but when I didn't, I had no idea what's wrong)
Also if someone had a guide explaining where to insert pluses and apostrophes in situations like this, it would be really helpful (I tried to search for one but never found it)
PS: The code above of course was in the resize function, it didn't work. Just saying in case it wasn't clear
Thanks in advance for help
Remove the wrapping (...) around your pixel value. There is also syntax error with your code. All you need is this:
$slideshowContainer.css('margin-left', ($slideshowContainer.width() / 2) + 'px');
If you are keen on using ES6 template literal syntax, it makes things a lot more readable:
$slideshowContainer.css('margin-left', `${$slideshowContainer.width() / 2}px`;
Try this:
$slideshowContainer.css('margin-left', + ($slideshowContainer.width() / 2) + 'px;');

javascript window.open url + json var

What I am trying to accomplish is to open a url(base) with an added var at the end.
window.open(https://vipparcel.com/package/detailed/ + json.id[0])
This was written by someone else to just display an image but I need it to pop more detailed info.
I DO NOT know javascript so its like a child poking at a dead animal here.
I have exhausted my normal go to's and then some to find more info on this.
the url would look like https://vipparcel.com/package/detailed/000000 if the info passes correctly.
Anything pointing me in the right direction would be extremely helpful!
Thank you in advanced.
Try this :
var myurl = "https://vipparcel.com/package/detailed/" + json.id[0];
window.open(myurl);
What errors are you getting? Looks like you're just missing quotations. If that's not the case, is json an object with an array of ids? If instead it's an array of objects with an id property, the following should work:
window.open('https://vipparcel.com/package/detailed/' + json[0].id)
It should look like this:
window.open('https://vipparcel.com/package/detailed/' + json.id[0]);
it's looking for a string to open the website by. You weren't passing a string through to it.

Adding 2 Values together in JS

So this probably pretty simple to do, but I'm now just learning this. I'm trying to add two values together to fill an input
Obviously that doesn't work, can someone please lend a hand?
There is a ; between the 2 values you are adding remove it like:
var summedValues = userInfo.fname + userInfo.lname;
In javascript ; means the end of the line and therefore it is not taking the consideration the + that is after it.

Href attribute using variable does not work

In JS, I am creating a link where the href attribute runs a function. The function simple opens an alert window with a given variable.
function alerter(str){
alert(str);
}
When I create the link, I use the following code.
var link = document.createElement('a');
link.setAttribute('href','javascript:alerter("hi")');
link.innerHTML = "Sauce";
If I do that, it works. However, what I want to do is alert a previously defined variable. But when i use
link.setAttribute('href','javascript:alerter(myvar)');
the popup no longer works. Also, is there a way to simply embed the alert in the href instead of writing a separate function?
It should be
link.setAttribute('href','javascript:alerter("' + myvar + '")');
You're using string concatenation to put myvar into that string. The way you had it, the whole thing is a string "javascript:alerter(myvar)"
Try this
link.setAttribute('href','javascript:alerter('+myvar+')');
The myvar variable has to be in the global scope. Be sure you can also access it as window.myvar.
for your first question use :
link.setAttribute('href','javascript:alerter(' + somevar + ')');
and for your second question use:
link.setAttribute('href','javascript:alert(' + somevar + ')');
cheers.
http://jsfiddle.net/xmBbw/2/
Just modify the onclick event of your anchor. If you are not going to link anywhere, you need not use the href attribute. Rather use onclick and assign any function you like to it. Besides, it's awfully bad practice to write javascript inside an html tag. Please don't.
A nice article regarding good practices

Why is 'indexOf' not returning anything?

The book I am reading tells me to open up the JavaScript console and try the code "foo: bar".indexOf(":"). I've tried it in many ways. I tried removing quotation marks, putting it inside a show() and alert() function. I just can't seem to tease anything out.
Has something changed in JavaScript? Has the author made a mistake? Am I supposed to get no return? Do I need to append document.write, perhaps? Any help greatly appreciated.
Yes something changed in Firefox 5+
However the console (ctrl-shift-k) still works
In the error console (ctrl-shift-J) you will need to wrap it in alert:
foo:bar is a property definition in json, and indexOf is supposed to deal with a left value (string variable, constant, or at least something that can have characters in it. I don't know why the book you are reading wants you to do this, but it doesn't seem to be correct. The correct way to use indexOf would be :
var myObject = {
foo:"bar"
}
alert(myObject.foo.indexOf("a"));
try like follows, it should work. Generally the indexOf() will return -1 if the value to search for never occurs.
var str="foo:bar";
document.write(str.indexOf(":") + "<br />");
The output should be 3

Categories

Resources