Solved

Hack or clean my hacked Javascript

Posted on 2013-01-14
8
283 Views
Last Modified: 2013-01-16
Hiya all

I have hacked this code together to get the path of the current URL and add it to a new url and made the browser request the new URL.

I was wondering if you code ninjas could do this more simply or clean er :)

<script>
var getLocation = function(href) {
    var l = document.createElement("a");
    l.href = href;
    return l;
};
var l = getLocation(window.location.href);
console.debug(l.pathname)

var img = document.createElement("img");
img.onload = function(e) {
	var container = document.getElementById("imageContainer");
	container.appendChild(e.target);
}
var imgurl = "http://site.comr?r=@page_" + l.pathname
img.setAttribute("src",imgurl);

document.write(l.pathname);

</script>
<div id="imageContainer"></div>

Open in new window

0
Comment
Question by:willa666
  • 3
  • 3
  • 2
8 Comments
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 38778219
What exactly are you trying to accomplish?

( for starters, get rif of the first function, it's redundant )

<script>
var l = window.location;
console.debug(l.pathname)

var img = document.createElement("img");
img.onload = function(e) {
	var container = document.getElementById("imageContainer");
	container.appendChild(e.target);
}
var imgurl = "http://site.comr?r=@page_" + l.pathname
img.setAttribute("src",imgurl);

document.write(l.pathname);
</script>

Open in new window

0
 
LVL 1

Author Comment

by:willa666
ID: 38778291
Hiya kozaiwaniec

I want to run the code on a page ( site.com/dir/page.html )

Extract the directory for the URL ( /dir/page.html )

Then call an image with a variable ( othersite.com/imagename.png?r=/dir/page.html )

Does that explain it?

wW
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 38778397
do you mean display the image inside the #imageContainer?
0
 
LVL 1

Author Comment

by:willa666
ID: 38778418
really i just have to request the image. I put it in the div so that i could confirm that the image was requested. :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 38778470
OK.

Then I think your script is good to go without that first function like I posted above.

that's me 2 cents :)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38780345
This would do it

<script>
var l = window.location;
var img = new Image().src= "http://site.comr?r=@page_" + l.pathname;
</script>

If you want debugging:


<script>
var l = window.location;
var img = new Image();
img.onload=function() { console.log(this.src+' was loaded'); }
img.onerror=function() { console.log(this.src+' was not loaded') };
img.src= "http://site.comr?r=@page_" + l.pathname;
</script>
0
 
LVL 1

Author Comment

by:willa666
ID: 38782593
Hello mplungjan

Thank you for the code. Will this code actually request the constructed url from the browser?

Ww
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 38783122
What do you mean?

It will request the URL from the server and load it into the browser. Since you do not seem to need to display the actual image, that is all the code does. Requests the URL from server (or cache if loaded before)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now