How to properly reference local resources in HTML?

A leading slash tells the browser to start at the root directory. If you don’t have the leading slash, you’re referencing from the current directory. If you add two dots before the leading slash, it means you’re referencing the parent of the current directory. Take the following folder structure notice: the ROOT checkmark is green, … Read more

Get img src with PHP

Use a HTML parser like DOMDocument and then evaluate the value you’re looking for with DOMXpath: $html=”<img id=”12″ border=”0″ src=”https://stackoverflow.com/images/image.jpg” alt=”Image” width=”100″ height=”100″ />”; $doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); $src = $xpath->evaluate(“string(//img/@src)”); # “https://stackoverflow.com/images/image.jpg” Or for those who really need to save space: $xpath = new DOMXPath(@DOMDocument::loadHTML($html)); $src = $xpath->evaluate(“string(//img/@src)”); And … Read more

What is my script src URL?

Put this in the js file that needs to know it’s own url. Fully Qualified (eg http://www.example.com/js/main.js): var scriptSource = (function(scripts) { var scripts = document.getElementsByTagName(‘script’), script = scripts[scripts.length – 1]; if (script.getAttribute.length !== undefined) { return script.src } return script.getAttribute(‘src’, -1) }()); Or As it appears in source (eg /js/main.js): var scriptSource = (function() … Read more

Why can’t I do ?

It would be a security vulnerability if the client could request local file system files and then use JavaScript to figure out what’s in them. The only way around this is to build an extension in a browser. Firefox extensions and IE extensions can access local resources. Chrome is much more restrictive.