1

Please help me with extracting window.location.href from a button .... click function doesn't work... just sends me to the main page and does nothing... I want to find it inside of an iframe why there is (#xx)

<button type="button" value="Upgrade to level 2" class="green build" onclick="window.location.href = 'dorf2.php?а=32&amp;c=Wra'; return false;">
    <div class="button-container addHoverClick ">
        <div class="button-background">
            <div class="buttonStart">
                <div class="buttonEnd">
                    <div class="buttonMiddle"></div>
                </div>
            </div>
        </div>
        <div class="button-content">Upgrade to level 2</div>
    </div>
</button>

way I think to get is

var buttonLink = $('#xx').contents().find('button.green.build').attr('href');
//or
var buttonLink = $('#xx').contents().find('button.green.build').text().attr('href');

.attr('href') will not work here because of window.location.href it is not same I think .... please help me copy it inside of attribute

YakovL
  • 7,557
  • 12
  • 62
  • 102
Tim Erjavc
  • 11
  • 3
  • Not very clear what you are trying to accomplish. For starters is the iframe on same domain as page it is in? You can't access a cross origin iframe due to *"same origin policy"*. If it is same origin explain problem in more detail – charlietfl Aug 06 '18 at 20:12
  • Welcome to StackOverflow. I've edited your post to improve some formatting and grammar but it's still quite unclear and I suggest you to improve it so that we can understand it. Is it correct that you have a page with html shown above and you want to extract its `onclick` handler text and further extract the value that `window.location.href` is set to by the handler? Can you edit the html part? Why don't you just use `a href=...` instead of `button` with a handler which opens a page? – YakovL Aug 06 '18 at 21:57
  • Also, why are you expecting to get anything from `$('#xx')` while your button doesn't have an id at all? And what does this have to do with an iframe? (you haven't shown any code containing an iframe) Is it the iframe which has the `xx` id? If so please show that part of the code, too. Best regards – YakovL Aug 06 '18 at 21:59
  • I want to copy part of link inside onclick .... so I cen use it like http://(server url)/+onclick I copy my hole script down there – Tim Erjavc Aug 07 '18 at 14:12

2 Answers2

1

1) The href is an attribute, and its not set in button, you can add it by write custom attribute like "data-href='yourUrl'"

2) by jQuery you can access a button by a lot of selectors, and you can read here about selectors, and by selector you can do this:

$('#buttonId').data('href'); OR $('#buttonId').attr('data-href');

3) finally you can do your job by this code:

$('#buttonId').click(function(){window.location.href = $(this).data('href');});

4) about read content from ifream I think what you are doing is subject to the same origin policy. read this

5) If you ignore policy and ifream in your domain ...you can continue work by $("#iFrame").contents().find("#buttonId")....etc...dont forget (same origin policy)

Anees Hikmat Abu Hmiad
  • 3,460
  • 2
  • 19
  • 36
0

Here is my hole script ... just part for autobuuilding .....

hope you understand what I am trying to do

<html>
    <head><meta http-equiv="refresh" content=111111111>
            <base href="http://www.x1000.aspidanetwork.com/">
    <script  src="https://code.jquery.com/jquery-3.3.1.min.js"            integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="               crossorigin="anonymous"></script>
    <script src='https://cdn.rawgit.com/admsev/jquery-play-sound/master/jquery.playSound.js'></script>              
<script>
    var totsent=0;
var timer1;
var timer2;
var timer3;
var timer4;
var timer5;
var traintimer;

function tester(){

if($('#xx').contents().find('button.green.build').length > 0) {

    var link = "http://www.x1000.aspidanetwork.com/"+$('#xx').contents().find('button.green.build').attr('data-href');

     $('iframe#xx').attr('src', link); document.getElementById('xx').src = link;

     return;
    }


}

//$('#xx').contents().find('div#contentOuterContainer').find('map#clickareas').length
//$('#xx').contents().find('div#contentOuterContainer').find('img.building.g10').length



</script>    


    </head>
    <body>
        <button onclick="tester();" > START </button>  <button onclick="start();"> proba_train </button> 
      <div id=messages></div>
        proba<input id=pehota type="text"  value="35">
        <iframe id=xx name=xx with=90% height=95% src="http://www.x1000.aspidanetwork.com/build.php?id=32" style="height:90%;width:90%;"></iframe>






           </body>
Tim Erjavc
  • 11
  • 3