Solved

Hack or clean my hacked Javascript

Posted on 2013-01-14
8
282 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
Comment Utility
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
Comment Utility
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
Comment Utility
do you mean display the image inside the #imageContainer?
0
 
LVL 1

Author Comment

by:willa666
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 25

Expert Comment

by:Kyle Hamilton
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

743 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

16 Experts available now in Live!

Get 1:1 Help Now