Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Hack or clean my hacked Javascript

Posted on 2013-01-14
8
Medium Priority
?
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

721 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