Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Hack or clean my hacked Javascript

Posted on 2013-01-14
8
Medium Priority
?
291 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

879 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