Solved

Hack or clean my hacked Javascript

Posted on 2013-01-14
8
288 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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

631 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