Kasper
Kasper

Reputation: 13622

Use url name to set name of image

I'm not sure if this is possible, but I want to do the following:

I've got webpages, called 1.html , 2.html , 3.html etc. Now I have the following code for 1.html:

<object width="100%" data="1.svg" type="image/svg+xml">

and for 2.html, I would get 2.svg. etc. Now I want to make something like 100 pages like this, and I'm wondering if it is possible to do this automaticly using javascript or jquery ? So something like:

<object width="100%" data="[here some script that get the name of htm file].svg" type="image/svg+xml">

Any ideas ?

Upvotes: 0

Views: 114

Answers (2)

musicnothing
musicnothing

Reputation: 3985

Try this:

jQuery

var url = document.location.pathname;
url = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
$('object').attr('data', url + '.svg');

Javascript

var url = document.location.pathname;
url = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
document.getElementsByTagName('object')[0].setAttribute('data',url + '.svg');

Or this:

PHP

<object data="<?php echo basename(__FILE__, ".htm"); ?>.svg"></object>

Upvotes: 1

raam86
raam86

Reputation: 6871

use

var currentDoc = document.location.pathname
var currentSVG = currentDoc.match(/\d+\) + ".svg";

Upvotes: 1

Related Questions