How to Convert this dynamic content in to an javascript array? - javascript

Hello everyone i would like to do this. I have a javascript variable which has this kind of value [section][row][/row][/section] I would like to convert this into an javascript array. do anyone has any clue about this. I just like an array like
[0]=>[section]
[1]=>[row]
[2]=>[/row]
[3]=>[/section]
Please Help me to do this.

Using some regex:
var tags = '[section][row][/row][/section]'.match(/\[\/?\w+]/g);
tags; // ["[section]", "[row]", "[/row]", "[/section]"]

Related

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.

Javascript/HTML assigning a value

I am having a problem with some Javascript/HTML/CSS code. I am fairly new to creating websites, so please bear with me.
what I am ultimately trying to do is pull a dynamic value from javascript and use that to sort some divs (in a container). What I am thinking is I will assign the value to the Id and then pull those into an array to be sorted by tinysort. If there's a faster way to do this, let me know.
However, my first problem is putting the data into the id so it can be sorted. Would I do something like
document.getElementById(namesort).value = iterator;
or would I use something like myData?
Note: I don't want to display the value, I just want to use it to sort.
Please ask for clarification if needed! Thanks in advance. :)
Here is the applicable code to this problem. http://jsfiddle.net/dw77hLyp/1/
It just basically shows a very basic outline of some of my code.
Without JQuery. You can create your attribute :
document.getElementById(namesort).createAttribute('myData');
document.getElementById(namesort).setAttribute("myData","hello Im in");
Pure JS Solution:
for( i = 0 ; i < iterator.length ; i++ ) {
document.getElementsByName('namesort')[i].setAttribute('data-sort',iterator[i]);
}
That way you add each of those elements an attribute called data-sort where the iterator as a value, then could create an array insert all the namesort elements to it, use the sorting algorithm and you're done.
document.getElementById("divID").innerHTML="someContent";

How to set the backgroundImage in javascript,my code dosn't work,Can any body help me?

The url of the image is a string variable,like this:
var links=document.getElementsByTagName("a");
var source = links[0].getAttribute("href");
var placeholder = document.getElementById("placeHolder");
placeholder.style.background="url(source)"; //likely to go wrong here
I'll be really grateful if somebody can help me.
unlike php, javascript doesn't recognize variables inside "" strings, so you have to do:
placeholder.style.background="url("+source+")";
This way you have the variable outside the string and then you have the concatenate (+) operator to join them.
I know the question was about javascript, but when making a website, using lots of javascript is a no-no. Vandervals is correct about how to set the background image with javascript, but here is an easier way to do it with CSS:
html {
background-image: url("your_image.jpg");
}
Regards,
Thomas

How to Split many Strings with Jquery

Let's say I have a bunch of strings which I get from user input. Each strings starts with a new line like the following example:
gordon:davis
harley:vona
empir:domen
furno:shyko
I would like to write a function in jQuery which loads the user data from the form and splits each row's sting. However I would ONLY need the first part of the strings, like:
gordon
harley
empir
furno
Is there any simple solution for this?
I have found something called: $.each(split) but I didn't get it work.
I'm sorry I'm a really newbie in Jquery, I hope you guys can help me out! Thanks in advance!
Use String.prototype.split method.
"gordon:davis".split(":") will return ["gordon","davis"]
Based on this, using Array.prototype.map or a JQuery version :
var strings = ["gordon:davis", "harley:vona"];
var splitStrings = strings.map(function (string) {
return string.split(":")[0];
});

Getting part of referring url with jquery

I'm trying to set a cookie with a value that is made up of part of a referring url. The referring url looks something like:
http://webct.university.ac.uk/webct/urw/lc715920578061.tp721521425061/courseMenu.dowebct?
and I need to extract:
lc715920578061.tp721521425061
for reuse in another function.
Would this be regex? Could anyone help with the syntax?
Many thanks
Matthew
You can use replace with a regex and split like this:
var desired_part = document.referrer.replace(/^https?:\/\//gi, '').split('/')[3];

Categories

Resources