Monday, May 25, 2015

Using the console

So it's been a while since i wrote something. I've been a little bit busy. It's not easy to coordinate the time i have. I must learn every day, but i have to go to the gym too. Because it gives me some push and refreshes me even if i used to feel myself tired. And there's my band, we should practice our new song and put a new one together. There's a lot of stuff to do but i could manage it sofar.
Now let's go to the issue i wrote recently about. It was a bit tricky 'cause the links with the downloadable document were hidden inside in a div element which can be opened with an anchor tag. So i couldn't just grab the anchor tags on the actual page because they were not the download links. And that's where some magic should happen. Ok, i know for a senior developer or even for a junior it's easy as a sunday morning (thanks Lionel and Faith no more). But for me it was a good practice. Before i get into any further, here's my code.




The anchor tags i needed were inside div elements with the class of document_txt. But there was also some tags i didn't needed. They had the class of infolink. So i took them away from the object with the not method. Then i had to scan through all the elements of the object that i just created. I used the each method for that. It has a callback function and inside of this callback i put all the code that does the right thing for me with every element of the object. First i must create a local variable to get the id of the particular document. So i grabbed with $(this) the actual anchor element. Then i get the href attribute of it which containes the link to details of the document. That link looks like:



I wanted that id in the end of that link and at the same i should rewrite the link so instead of details there would be download. The split method creates from the link an array where every character would be an element of this array. Inside the parentheses we can specify where the method should separate the characters. In this case it's nothing because we want to make from every letter and number a different array element. Then i used the slice method. With that i could make a new array only with the numbers at the end of the link. That would be the id i needed. In the parentheses i gave to the method the index of the element where it should the count begin with (remember an array is always zero based, so the first element of the array has the index of 0). And secondly the number where the count should end. But that's not enough 'cause we can't use that as an array, we need the string to put that at the end of the new link which comes in the next line of the code. I used the join method to put the numbers together to get a string again. It'll join these characters together with the element which we write between the parentheses. In this case again it'll be nothing, because we want the numbers to come right after each other. Good, now this string of the id is stored in the variable with the name of id. The next local variable would be the link which would create the new href attribute for every anchor tag that it runs through. Like before we grab the actual element with $(this) then again with the attr method we can set the new href attribute. It can be done if as a second parameter we pass in some string in. That string would be ofcourse the new link and at the end of it the particular id. Notice that the id isn't hardcoded, we add it dynamically to every piece of the object. And with the last line of code we just open every new link that we have created. We can do that with the javascript window.open method. As parameter we pass in the local variable. As a result we have some tabs inside the browser opened where the download of all the documents begins.
I must say that this worked for me only in firefox. I've tried it in chrome but there somehow it didn't. I'll be working on that. But if you have some idea what could be the problem please let me know.

No comments:

Post a Comment